@@ -2,6 +2,8 @@ package export
22
33import (
44 "fmt"
5+ "path/filepath"
6+ "strings"
57
68 "github.com/stackitcloud/stackit-cli/internal/pkg/args"
79 "github.com/stackitcloud/stackit-cli/internal/pkg/config"
@@ -17,12 +19,14 @@ const (
1719 profileNameArg = "PROFILE_NAME"
1820
1921 filePathFlag = "file-path"
22+
23+ configFileExtension = "json"
2024)
2125
2226type inputModel struct {
2327 * globalflags.GlobalFlagModel
2428 ProfileName string
25- ExportPath string
29+ FilePath string
2630}
2731
2832func NewCmd (p * print.Printer ) * cobra.Command {
@@ -32,7 +36,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
3236 Long : "Exports a CLI configuration profile." ,
3337 Example : examples .Build (
3438 examples .NewExample (
35- `Export a profile with name "PROFILE_NAME" to the current path ` ,
39+ `Export a profile with name "PROFILE_NAME" to a file in your current directory ` ,
3640 "$ stackit config profile export PROFILE_NAME" ,
3741 ),
3842 examples .NewExample (
@@ -47,12 +51,12 @@ func NewCmd(p *print.Printer) *cobra.Command {
4751 return err
4852 }
4953
50- err = config .ExportProfile (p , model .ProfileName , model .ExportPath )
54+ err = config .ExportProfile (p , model .ProfileName , model .FilePath )
5155 if err != nil {
5256 return fmt .Errorf ("could not export profile: %w" , err )
5357 }
5458
55- p .Info ("Exported profile %q\n " , model .ProfileName )
59+ p .Info ("Exported profile %q to %q \n " , model .ProfileName , model . FilePath )
5660
5761 return nil
5862 },
@@ -62,7 +66,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
6266}
6367
6468func configureFlags (cmd * cobra.Command ) {
65- cmd .Flags ().String (filePathFlag , "" , "Path where the config should be saved. E.g. '--file-path ~/config.json', '--file-path ~/'" )
69+ cmd .Flags ().StringP (filePathFlag , "f " , "" , "If set, writes the payload to the given. If unset, writes the payload to you current directory with the name of the profile. E.g. '--file-path ~/my- config.json', '--file-path ~/'" )
6670}
6771
6872func parseInput (p * print.Printer , cmd * cobra.Command , inputArgs []string ) (* inputModel , error ) {
@@ -72,7 +76,13 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu
7276 model := inputModel {
7377 GlobalFlagModel : globalFlags ,
7478 ProfileName : profileName ,
75- ExportPath : flags .FlagToStringValue (p , cmd , filePathFlag ),
79+ FilePath : flags .FlagToStringValue (p , cmd , filePathFlag ),
80+ }
81+
82+ // If filePath contains does not contain a file name, then add a default name
83+ if ! strings .HasSuffix (model .FilePath , fmt .Sprintf (".%s" , configFileExtension )) {
84+ exportFileName := fmt .Sprintf ("%s.%s" , model .ProfileName , configFileExtension )
85+ model .FilePath = filepath .Join (model .FilePath , exportFileName )
7686 }
7787
7888 if p .IsVerbosityDebug () {
0 commit comments