-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(CLI): Support context Signed-off-by: Ce Gao <cegao@tensorchord.ai> * fix: Add remove and fix test cases Signed-off-by: Ce Gao <cegao@tensorchord.ai>
- Loading branch information
Showing
21 changed files
with
696 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright 2022 The envd Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package app | ||
|
||
import ( | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
var CommandContext = &cli.Command{ | ||
Name: "context", | ||
Usage: "Manage envd contexts", | ||
Subcommands: []*cli.Command{ | ||
CommandContextCreate, | ||
CommandContextList, | ||
CommandContextRemove, | ||
CommandContextUse, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright 2022 The envd Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package app | ||
|
||
import ( | ||
"github.com/cockroachdb/errors" | ||
"github.com/sirupsen/logrus" | ||
"github.com/tensorchord/envd/pkg/home" | ||
"github.com/tensorchord/envd/pkg/types" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
var CommandContextCreate = &cli.Command{ | ||
Name: "create", | ||
Usage: "Create envd context", | ||
Flags: []cli.Flag{ | ||
&cli.StringFlag{ | ||
Name: "name", | ||
Usage: "Name of the context", | ||
Value: "", | ||
}, | ||
&cli.StringFlag{ | ||
Name: "builder", | ||
Usage: "Builder to use (docker-container, kube-pod)", | ||
Value: string(types.BuilderTypeDocker), | ||
}, | ||
&cli.StringFlag{ | ||
Name: "builder-name", | ||
Usage: "Builder name", | ||
Value: "envd_buildkitd", | ||
}, | ||
&cli.BoolFlag{ | ||
Name: "use", | ||
Usage: "Use the context", | ||
}, | ||
}, | ||
Action: contextCreate, | ||
} | ||
|
||
func contextCreate(clicontext *cli.Context) error { | ||
name := clicontext.String("name") | ||
builder := clicontext.String("builder") | ||
builderName := clicontext.String("builder-name") | ||
use := clicontext.Bool("use") | ||
|
||
err := home.GetManager().ContextCreate(name, | ||
types.BuilderType(builder), builderName, use) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to create context") | ||
} | ||
logrus.Infof("Context %s is created", name) | ||
if use { | ||
logrus.Infof("Current context is now \"%s\"", name) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright 2022 The envd Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package app | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
"github.com/cockroachdb/errors" | ||
"github.com/olekukonko/tablewriter" | ||
"github.com/urfave/cli/v2" | ||
|
||
"github.com/tensorchord/envd/pkg/home" | ||
"github.com/tensorchord/envd/pkg/types" | ||
) | ||
|
||
var CommandContextList = &cli.Command{ | ||
Name: "ls", | ||
Usage: "List envd contexts", | ||
Action: contextList, | ||
} | ||
|
||
func contextList(clicontext *cli.Context) error { | ||
contexts, err := home.GetManager().ContextList() | ||
if err != nil { | ||
return errors.Wrap(err, "failed to list context") | ||
} | ||
renderContext(contexts, os.Stdout) | ||
return nil | ||
} | ||
|
||
func renderContext(contexts types.EnvdContext, w io.Writer) { | ||
table := tablewriter.NewWriter(w) | ||
table.SetHeader([]string{"context", "builder", "socket"}) | ||
|
||
table.SetAutoWrapText(false) | ||
table.SetAutoFormatHeaders(true) | ||
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT) | ||
table.SetAlignment(tablewriter.ALIGN_LEFT) | ||
table.SetCenterSeparator("") | ||
table.SetColumnSeparator("") | ||
table.SetRowSeparator("") | ||
table.SetHeaderLine(false) | ||
table.SetBorder(false) | ||
table.SetTablePadding("\t") // pad with tabs | ||
table.SetNoWhiteSpace(true) | ||
|
||
for _, p := range contexts.Contexts { | ||
envRow := make([]string, 3) | ||
if p.Name == contexts.Current { | ||
envRow[0] = fmt.Sprintf("%s (current)", p.Name) | ||
} else { | ||
envRow[0] = p.Name | ||
} | ||
envRow[1] = string(p.Builder) | ||
envRow[2] = fmt.Sprintf("%s://%s", p.Builder, p.BuilderSocket) | ||
table.Append(envRow) | ||
} | ||
table.Render() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright 2022 The envd Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package app | ||
|
||
import ( | ||
"github.com/cockroachdb/errors" | ||
"github.com/sirupsen/logrus" | ||
"github.com/tensorchord/envd/pkg/home" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
var CommandContextRemove = &cli.Command{ | ||
Name: "rm", | ||
Usage: "Remove envd context", | ||
Flags: []cli.Flag{ | ||
&cli.StringFlag{ | ||
Name: "name", | ||
Usage: "Name of the context", | ||
Value: "", | ||
}, | ||
}, | ||
Action: contextRemove, | ||
} | ||
|
||
func contextRemove(clicontext *cli.Context) error { | ||
name := clicontext.String("name") | ||
|
||
err := home.GetManager().ContextRemove(name) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to remove context") | ||
} | ||
logrus.Infof("Context %s is removed", name) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright 2022 The envd Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package app | ||
|
||
import ( | ||
"github.com/cockroachdb/errors" | ||
"github.com/sirupsen/logrus" | ||
"github.com/tensorchord/envd/pkg/home" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
var CommandContextUse = &cli.Command{ | ||
Name: "use", | ||
Usage: "Use the specified envd context", | ||
Flags: []cli.Flag{ | ||
&cli.StringFlag{ | ||
Name: "name", | ||
Usage: "Name of the context", | ||
Value: "", | ||
}, | ||
}, | ||
Action: contextUse, | ||
} | ||
|
||
func contextUse(clicontext *cli.Context) error { | ||
name := clicontext.String("name") | ||
|
||
err := home.GetManager().ContextUse(name) | ||
if err != nil { | ||
return errors.Wrapf(err, "failed to use the specified context %s", name) | ||
} | ||
logrus.Infof("Current context is now \"%s\"", name) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.