Skip to content
This repository has been archived by the owner on Dec 26, 2023. It is now read-only.

Commit

Permalink
Add select context
Browse files Browse the repository at this point in the history
  • Loading branch information
rafiramadhana committed Sep 17, 2023
1 parent 9b1475e commit 540ed40
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ jobs:
layerform config set-context test-cloud -t cloud --url https://demo.layerform.dev --email [email protected] --password strongpass
layerform config set-context test-local -t local --dir test
- name: layerform config set-context
run: |
# fails if command succeeds
! layerform config select-context test-does-not-exist # context does not exist
layerform config set-context test-local -t local --dir test && layerform config select-context test-local
- name: Configure
run: |
layerform configure --file examples/local/layerform.json
Expand Down
55 changes: 55 additions & 0 deletions cmd/cli/select_context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package cli

import (
"fmt"
"os"

"github.com/ergomake/layerform/internal/lfconfig"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

func init() {
configCmd.AddCommand(configSelectContextCmd)
}

var configSelectContextCmd = &cobra.Command{
Use: "select-context <name>",
Short: "Select a context entry from layerform config file",
Long: `Select a context entry from layerform config file.
Selecting a name that does not exist will return error.`,
Example: `# Select a context
layerform config select-context local-example`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
name := args[0]

cfg, err := lfconfig.Load("")
if err != nil && !errors.Is(err, os.ErrNotExist) {
fmt.Fprintf(os.Stderr, "%s\n", errors.Wrap(err, "fail to open config file"))
os.Exit(1)
}

_, ok := cfg.Contexts[name]
if !ok {
fmt.Fprintf(
os.Stderr,
"context %s does not exist\n",
name,
)
os.Exit(1)
}

cfg.CurrentContext = name

err = cfg.Save()
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", errors.Wrap(err, "fail to save config file"))
os.Exit(1)
}

fmt.Fprintf(os.Stdout, "Context \"%s\" selected.\n", name)
},
SilenceErrors: true,
}

0 comments on commit 540ed40

Please sign in to comment.