-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dmitrii Shelomentsev
committed
Mar 26, 2024
1 parent
f870dbf
commit 2c78fea
Showing
22 changed files
with
1,023 additions
and
98 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/G-core/gcore-cli/internal/commands" | ||
"github.com/G-core/gcore-cli/internal/core" | ||
) | ||
|
||
func main() { | ||
core.Execute() | ||
core.Execute(commands.Commands()) | ||
} |
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,17 @@ | ||
package commands | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/G-core/gcore-cli/internal/commands/config" | ||
"github.com/G-core/gcore-cli/internal/commands/fastedge" | ||
initCmd "github.com/G-core/gcore-cli/internal/commands/init" | ||
) | ||
|
||
func Commands() []*cobra.Command { | ||
return []*cobra.Command{ | ||
fastedge.Commands(), | ||
initCmd.Commands(), | ||
config.Commands(), | ||
} | ||
} |
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,149 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"slices" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/G-core/gcore-cli/internal/config" | ||
"github.com/G-core/gcore-cli/internal/core" | ||
"github.com/G-core/gcore-cli/internal/output" | ||
) | ||
|
||
func Commands() *cobra.Command { | ||
var cmd = &cobra.Command{ | ||
Use: "config", | ||
Short: "Config file management", | ||
GroupID: "configuration", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
cmd.Help() | ||
}, | ||
} | ||
|
||
cmd.AddCommand(info(), get(), set(), unset(), dump(), profileCmd()) | ||
return cmd | ||
} | ||
|
||
func profileCmd() *cobra.Command { | ||
var cmd = &cobra.Command{ | ||
Use: "profile", | ||
Short: "Commands to manage profiles from the config", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
cmd.Help() | ||
}, | ||
} | ||
|
||
cmd.AddCommand(listProfiles(), switchProfileCmd(), deleteProfileCmd()) | ||
return cmd | ||
} | ||
|
||
func deleteProfileCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "delete <profile>", | ||
Aliases: []string{"d"}, | ||
ValidArgsFunction: core.ProfileCompletion, | ||
Short: "Delete profile from the config", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if len(args) == 0 { | ||
cmd.Help() | ||
|
||
return nil | ||
} | ||
|
||
profileName := args[0] | ||
ctx := cmd.Context() | ||
cfg := core.ExtractConfig(ctx) | ||
active := core.ExtractProfile(ctx) | ||
|
||
_, exist := cfg.Profiles[profileName] | ||
if exist { | ||
delete(cfg.Profiles, profileName) | ||
} else { | ||
return fmt.Errorf("profile '%s' doesn't exist", profileName) | ||
} | ||
|
||
if active == profileName { | ||
cfg.ActiveProfile = config.DefaultProfile | ||
} | ||
|
||
path, err := core.ExtractConfigPath(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return cfg.Save(path) | ||
}, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func listProfiles() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "list", | ||
Aliases: []string{"ls"}, | ||
Short: "Display list of available profiles in the config", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
ctx := cmd.Context() | ||
cfg := core.ExtractConfig(ctx) | ||
|
||
profiles := append([]profileView{}, toProfileView(config.DefaultProfile, &cfg.Profile)) | ||
|
||
var names []string | ||
for name, _ := range cfg.Profiles { | ||
names = append(names, name) | ||
} | ||
slices.Sort(names) | ||
|
||
for _, name := range names { | ||
pv := toProfileView(name, cfg.Profiles[name]) | ||
|
||
profiles = append(profiles, pv) | ||
} | ||
|
||
output.Print(profiles) | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func switchProfileCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "switch <profile>", | ||
ValidArgsFunction: core.ProfileCompletion, | ||
Short: "Make selected profile active", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if len(args) == 0 { | ||
cmd.Help() | ||
|
||
return nil | ||
} | ||
|
||
profileName := args[0] | ||
ctx := cmd.Context() | ||
cfg := core.ExtractConfig(ctx) | ||
|
||
_, exist := cfg.Profiles[profileName] | ||
if exist { | ||
cfg.ActiveProfile = profileName | ||
} else if profileName != config.DefaultProfile { | ||
return fmt.Errorf("profile '%s' doesn't exist", profileName) | ||
} else { | ||
cfg.ActiveProfile = config.DefaultProfile | ||
} | ||
|
||
path, err := core.ExtractConfigPath(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return cfg.Save(path) | ||
}, | ||
} | ||
|
||
return cmd | ||
} |
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,33 @@ | ||
package config | ||
|
||
import ( | ||
"github.com/AlekSi/pointer" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/G-core/gcore-cli/internal/core" | ||
"github.com/G-core/gcore-cli/internal/output" | ||
) | ||
|
||
func dump() *cobra.Command { | ||
var cmd = &cobra.Command{ | ||
Use: "dump", | ||
Short: "Dumps the config file", | ||
Args: cobra.NoArgs, | ||
RunE: func(cmd *cobra.Command, args []string) (err error) { | ||
ctx := cmd.Context() | ||
cfg := core.ExtractConfig(ctx) | ||
|
||
// Secure keys | ||
cfg.Profile.ApiKey = pointer.To(secureKey(cfg.Profile.ApiKey)) | ||
for _, profile := range cfg.Profiles { | ||
profile.ApiKey = pointer.To(secureKey(profile.ApiKey)) | ||
} | ||
|
||
output.Print(cfg) | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
return cmd | ||
} |
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,74 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
|
||
"github.com/iancoleman/strcase" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/G-core/gcore-cli/internal/config" | ||
"github.com/G-core/gcore-cli/internal/core" | ||
"github.com/G-core/gcore-cli/internal/output" | ||
) | ||
|
||
func getProfileField(profile *config.Profile, key string) (reflect.Value, error) { | ||
field := reflect.ValueOf(profile).Elem().FieldByName(strcase.ToCamel(key)) | ||
reflect.ValueOf(profile).Elem().FieldByNameFunc(func(s string) bool { | ||
return key == strcase.ToKebab(s) | ||
}) | ||
|
||
if !field.IsValid() { | ||
return reflect.ValueOf(nil), fmt.Errorf("invalid key: %s", key) | ||
} | ||
|
||
return field, nil | ||
} | ||
|
||
func getProfileValue(profile *config.Profile, fieldName string) (interface{}, error) { | ||
field, err := getProfileField(profile, fieldName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return field.Interface(), nil | ||
} | ||
|
||
func get() *cobra.Command { | ||
var cmd = &cobra.Command{ | ||
Use: "get <property>", | ||
Short: "Get property value from the config file", | ||
ValidArgs: []string{"api-url", "api-key"}, | ||
Args: func(cmd *cobra.Command, args []string) error { | ||
if len(args) == 0 { | ||
cmd.Help() | ||
} | ||
|
||
return nil | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if len(args) == 0 { | ||
return nil | ||
} | ||
|
||
ctx := cmd.Context() | ||
profileName := core.ExtractProfile(ctx) | ||
cfg := core.ExtractConfig(ctx) | ||
|
||
profile, err := cfg.GetProfile(profileName) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
value, err := getProfileValue(profile, args[0]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
output.Print(value) | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
return cmd | ||
} |
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,69 @@ | ||
package config | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/AlekSi/pointer" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/G-core/gcore-cli/internal/config" | ||
"github.com/G-core/gcore-cli/internal/core" | ||
"github.com/G-core/gcore-cli/internal/output" | ||
) | ||
|
||
type profileView struct { | ||
Name string | ||
ApiUrl *string | ||
ApiKey *string | ||
} | ||
|
||
func toProfileView(name string, profile *config.Profile) profileView { | ||
var pv = profileView{ | ||
Name: name, | ||
} | ||
|
||
if profile.ApiUrl != nil { | ||
pv.ApiUrl = profile.ApiUrl | ||
} | ||
|
||
if profile.ApiKey != nil { | ||
pv.ApiKey = pointer.To(secureKey(profile.ApiKey)) | ||
} | ||
|
||
return pv | ||
} | ||
|
||
func info() *cobra.Command { | ||
var cmd = &cobra.Command{ | ||
Use: "info", | ||
Short: "Get information about config profile", | ||
Args: cobra.NoArgs, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
ctx := cmd.Context() | ||
profile, err := core.GetClientProfile(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
output.Print(toProfileView(core.ExtractProfile(ctx), profile)) | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func secureKey(key *string) string { | ||
if key == nil || *key == "" { | ||
return "" | ||
} | ||
|
||
var p1 = 0 + 5 | ||
var p2 = len(*key) - 1 - 5 | ||
if p1 > p2 { | ||
return "XXXXXX" | ||
} | ||
|
||
return strings.Join([]string{(*key)[0:p1], "XXXXXX", (*key)[p2 : len((*key))-1]}, "") | ||
} |
Oops, something went wrong.