Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions docs/stackit_config.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ Provides functionality for CLI configuration options

### Synopsis

Provides functionality for CLI configuration options
The configuration is stored in a file in the user's config directory, which is OS dependent.
Windows: %APPDATA%\stackit
Linux: $XDG_CONFIG_HOME/stackit
macOS: $HOME/Library/Application Support/stackit
The configuration file is named `cli-config.json` and is created automatically in your first CLI run.
Provides functionality for CLI configuration options.
You can set and unset different configuration options via the "stackit config set" and "stackit config unset" commands.

Additionally, you can configure the CLI to use different profiles, each with its own configuration.
Additional profiles can be configured via the "STACKIT_CLI_PROFILE" environment variable or using the "stackit config profile set PROFILE" and "stackit config profile unset" commands.
The environment variable takes precedence over what is set via the commands.

```
stackit config [flags]
Expand All @@ -35,6 +35,7 @@ stackit config [flags]

* [stackit](./stackit.md) - Manage STACKIT resources using the command line
* [stackit config list](./stackit_config_list.md) - Lists the current CLI configuration values
* [stackit config profile](./stackit_config_profile.md) - Manage the CLI configuration profiles
* [stackit config set](./stackit_config_set.md) - Sets CLI configuration options
* [stackit config unset](./stackit_config_unset.md) - Unsets CLI configuration options

37 changes: 37 additions & 0 deletions docs/stackit_config_profile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
## stackit config profile

Manage the CLI configuration profiles

### Synopsis

Manage the CLI configuration profiles.
The profile to be used can be managed via the "STACKIT_CLI_PROFILE" environment variable or using the "stackit config profile set PROFILE" and "stackit config profile unset" commands.
The environment variable takes precedence over what is set via the commands.
When no profile is set, the default profile is used.

```
stackit config profile [flags]
```

### Options

```
-h, --help Help for "stackit config profile"
```

### Options inherited from parent commands

```
-y, --assume-yes If set, skips all confirmation prompts
--async If set, runs the command asynchronously
-o, --output-format string Output format, one of ["json" "pretty" "none"]
-p, --project-id string Project ID
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
```

### SEE ALSO

* [stackit config](./stackit_config.md) - Provides functionality for CLI configuration options
* [stackit config profile set](./stackit_config_profile_set.md) - Set a CLI configuration profile
* [stackit config profile unset](./stackit_config_profile_unset.md) - Unset the current active CLI configuration profile

43 changes: 43 additions & 0 deletions docs/stackit_config_profile_set.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
## stackit config profile set

Set a CLI configuration profile

### Synopsis

Set a CLI configuration profile as the active profile.
The profile to be used can be managed via the STACKIT_CLI_PROFILE environment variable or using the "stackit config profile set PROFILE" and "stackit config profile unset" commands.
The environment variable takes precedence over what is set via the commands.
A new profile is created automatically if it does not exist.
When no profile is set, the default profile is used.

```
stackit config profile set PROFILE [flags]
```

### Examples

```
Set the configuration profile "my-profile" as the active profile
$ stackit config profile set my-profile
```

### Options

```
-h, --help Help for "stackit config profile set"
```

### Options inherited from parent commands

```
-y, --assume-yes If set, skips all confirmation prompts
--async If set, runs the command asynchronously
-o, --output-format string Output format, one of ["json" "pretty" "none"]
-p, --project-id string Project ID
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
```

### SEE ALSO

* [stackit config profile](./stackit_config_profile.md) - Manage the CLI configuration profiles

40 changes: 40 additions & 0 deletions docs/stackit_config_profile_unset.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
## stackit config profile unset

Unset the current active CLI configuration profile

### Synopsis

Unset the current active CLI configuration profile.
When no profile is set, the default profile will be used.

```
stackit config profile unset [flags]
```

### Examples

```
Unset the currently active configuration profile. The default profile will be used.
$ stackit config profile unset
```

### Options

```
-h, --help Help for "stackit config profile unset"
```

### Options inherited from parent commands

```
-y, --assume-yes If set, skips all confirmation prompts
--async If set, runs the command asynchronously
-o, --output-format string Output format, one of ["json" "pretty" "none"]
-p, --project-id string Project ID
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
```

### SEE ALSO

* [stackit config profile](./stackit_config_profile.md) - Manage the CLI configuration profiles

Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,4 @@ stackit load-balancer observability-credentials cleanup [flags]

### SEE ALSO

* [stackit load-balancer observability-credentials](./stackit_load-balancer_observability-credentials.md) - Provides functionality for Load Balancer observability credentials

- [stackit load-balancer observability-credentials](./stackit_load-balancer_observability-credentials.md) - Provides functionality for Load Balancer observability credentials
14 changes: 8 additions & 6 deletions internal/cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/stackitcloud/stackit-cli/internal/cmd/config/list"
"github.com/stackitcloud/stackit-cli/internal/cmd/config/profile"
"github.com/stackitcloud/stackit-cli/internal/cmd/config/set"
"github.com/stackitcloud/stackit-cli/internal/cmd/config/unset"
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
Expand All @@ -17,12 +18,12 @@ func NewCmd(p *print.Printer) *cobra.Command {
cmd := &cobra.Command{
Use: "config",
Short: "Provides functionality for CLI configuration options",
Long: fmt.Sprintf("%s\n%s\n%s\n%s\n%s\n%s", "Provides functionality for CLI configuration options",
"The configuration is stored in a file in the user's config directory, which is OS dependent.",
"Windows: %APPDATA%\\stackit",
"Linux: $XDG_CONFIG_HOME/stackit",
"macOS: $HOME/Library/Application Support/stackit",
"The configuration file is named `cli-config.json` and is created automatically in your first CLI run.",
Long: fmt.Sprintf("%s\n%s\n\n%s\n%s\n%s",
"Provides functionality for CLI configuration options.",
`You can set and unset different configuration options via the "stackit config set" and "stackit config unset" commands.`,
"Additionally, you can configure the CLI to use different profiles, each with its own configuration.",
`Additional profiles can be configured via the "STACKIT_CLI_PROFILE" environment variable or using the "stackit config profile set PROFILE" and "stackit config profile unset" commands.`,
"The environment variable takes precedence over what is set via the commands.",
),
Args: args.NoArgs,
Run: utils.CmdHelp,
Expand All @@ -35,4 +36,5 @@ func addSubcommands(cmd *cobra.Command, p *print.Printer) {
cmd.AddCommand(list.NewCmd(p))
cmd.AddCommand(set.NewCmd(p))
cmd.AddCommand(unset.NewCmd(p))
cmd.AddCommand(profile.NewCmd(p))
}
17 changes: 15 additions & 2 deletions internal/cmd/config/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ func NewCmd(p *print.Printer) *cobra.Command {
configData := viper.AllSettings()

model := parseInput(p, cmd)
return outputResult(p, model.OutputFormat, configData)

activeProfile, err := config.GetProfile()
if err != nil {
return fmt.Errorf("get profile: %w", err)
}

return outputResult(p, model.OutputFormat, configData, activeProfile)
},
}
return cmd
Expand All @@ -65,9 +71,12 @@ func parseInput(p *print.Printer, cmd *cobra.Command) *inputModel {
}
}

func outputResult(p *print.Printer, outputFormat string, configData map[string]any) error {
func outputResult(p *print.Printer, outputFormat string, configData map[string]any, activeProfile string) error {
switch outputFormat {
case print.JSONOutputFormat:
if activeProfile != "" {
configData["profile"] = activeProfile
}
details, err := json.MarshalIndent(configData, "", " ")
if err != nil {
return fmt.Errorf("marshal config list: %w", err)
Expand All @@ -83,6 +92,7 @@ func outputResult(p *print.Printer, outputFormat string, configData map[string]a

return nil
default:

// Sort the config options by key
configKeys := make([]string, 0, len(configData))
for k := range configData {
Expand All @@ -91,6 +101,9 @@ func outputResult(p *print.Printer, outputFormat string, configData map[string]a
sort.Strings(configKeys)

table := tables.NewTable()
if activeProfile != "" {
table.SetTitle(fmt.Sprintf("Profile: %q", activeProfile))
}
table.SetHeader("NAME", "VALUE")
for _, key := range configKeys {
value := configData[key]
Expand Down
35 changes: 35 additions & 0 deletions internal/cmd/config/profile/profile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package profile

import (
"fmt"

"github.com/stackitcloud/stackit-cli/internal/cmd/config/profile/set"
"github.com/stackitcloud/stackit-cli/internal/cmd/config/profile/unset"
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"

"github.com/spf13/cobra"
)

func NewCmd(p *print.Printer) *cobra.Command {
cmd := &cobra.Command{
Use: "profile",
Short: "Manage the CLI configuration profiles",
Long: fmt.Sprintf("%s\n%s\n%s\n%s",
"Manage the CLI configuration profiles.",
`The profile to be used can be managed via the "STACKIT_CLI_PROFILE" environment variable or using the "stackit config profile set PROFILE" and "stackit config profile unset" commands.`,
"The environment variable takes precedence over what is set via the commands.",
"When no profile is set, the default profile is used.",
),
Args: args.NoArgs,
Run: utils.CmdHelp,
}
addSubcommands(cmd, p)
return cmd
}

func addSubcommands(cmd *cobra.Command, p *print.Printer) {
cmd.AddCommand(set.NewCmd(p))
cmd.AddCommand(unset.NewCmd(p))
}
94 changes: 94 additions & 0 deletions internal/cmd/config/profile/set/set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package set

import (
"fmt"

"github.com/stackitcloud/stackit-cli/internal/pkg/args"
"github.com/stackitcloud/stackit-cli/internal/pkg/auth"
"github.com/stackitcloud/stackit-cli/internal/pkg/config"
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"

"github.com/spf13/cobra"
)

const (
profileArg = "PROFILE"
)

type inputModel struct {
*globalflags.GlobalFlagModel
Profile string
}

func NewCmd(p *print.Printer) *cobra.Command {
cmd := &cobra.Command{
Use: fmt.Sprintf("set %s", profileArg),
Short: "Set a CLI configuration profile",
Long: fmt.Sprintf("%s\n%s\n%s\n%s\n%s",
"Set a CLI configuration profile as the active profile.",
`The profile to be used can be managed via the STACKIT_CLI_PROFILE environment variable or using the "stackit config profile set PROFILE" and "stackit config profile unset" commands.`,
"The environment variable takes precedence over what is set via the commands.",
"A new profile is created automatically if it does not exist.",
"When no profile is set, the default profile is used.",
),
Args: args.SingleArg(profileArg, nil),
Example: examples.Build(
examples.NewExample(
`Set the configuration profile "my-profile" as the active profile`,
"$ stackit config profile set my-profile"),
),
RunE: func(cmd *cobra.Command, args []string) error {
model, err := parseInput(p, cmd, args)
if err != nil {
return err
}

err = config.SetProfile(p, model.Profile)
if err != nil {
return fmt.Errorf("set profile: %w", err)
}

p.Info("Successfully set active profile to %q\n", model.Profile)

flow, err := auth.GetAuthFlow()
if err != nil {
p.Debug(print.WarningLevel, "both keyring and text file storage failed to find a valid authentication flow for the active profile")
p.Warn("The active profile %q is not authenticated, please login using the 'stackit auth login' command.\n", model.Profile)
return nil
}
p.Debug(print.DebugLevel, "found valid authentication flow for active profile: %s", flow)

return nil
},
}
return cmd
}

func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inputModel, error) {
profile := inputArgs[0]

err := config.ValidateProfile(profile)
if err != nil {
return nil, err
}

globalFlags := globalflags.Parse(p, cmd)

model := inputModel{
GlobalFlagModel: globalFlags,
Profile: profile,
}

if p.IsVerbosityDebug() {
modelStr, err := print.BuildDebugStrFromInputModel(model)
if err != nil {
p.Debug(print.ErrorLevel, "convert model to string for debugging: %v", err)
} else {
p.Debug(print.DebugLevel, "parsed input values: %s", modelStr)
}
}

return &model, nil
}
Loading