Skip to content

Commit

Permalink
added history network selector (#37)
Browse files Browse the repository at this point in the history
* working history network selector

* fixed test
  • Loading branch information
arontaubyte authored Aug 21, 2023
1 parent d1a81d4 commit 57c081e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
22 changes: 18 additions & 4 deletions cli/commands/resources/network/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/taubyte/tau-cli/singletons/config"
"github.com/taubyte/tau-cli/singletons/dreamland"
"github.com/taubyte/tau-cli/validate"
slices "github.com/taubyte/utils/slices/string"

networkFlags "github.com/taubyte/tau-cli/flags/network"
networkI18n "github.com/taubyte/tau-cli/i18n/network"
Expand All @@ -26,7 +27,6 @@ func (link) Select() cliCommon.Command {
)
}

// TODO: maybe save the old custom url when switching and prompt to use same as saved before
func _select(ctx *cli.Context) error {
// Setting string flag with value counts as two
if ctx.NumFlags() > 2 {
Expand All @@ -47,6 +47,10 @@ func _select(ctx *cli.Context) error {
return err
}

if !slices.Contains(profile.History, profile.Network) {
profile.History = append(profile.History, profile.Network)
}

case ctx.IsSet(networkFlags.Universe.Name):
dreamClient, err := dreamland.Client(ctx.Context)
if err != nil {
Expand Down Expand Up @@ -77,19 +81,26 @@ func _select(ctx *cli.Context) error {
networkSelections = append(networkSelections, common.DreamlandNetwork)
}

networkSelections = append(networkSelections, profile.History...)

prev := []string{}
if len(profile.NetworkType) > 0 {
prev = append(prev, profile.NetworkType)
}

profile.NetworkType = prompts.GetOrAskForSelection(ctx, "Network", prompts.NetworkPrompts, networkSelections, prev...)
if profile.NetworkType == common.RemoteNetwork {
network := prompts.GetOrAskForSelection(ctx, "Network", prompts.NetworkPrompts, networkSelections, prev...)
if network == common.RemoteNetwork {
profile.NetworkType = common.RemoteNetwork
profile.Network = prompts.GetOrRequireAString(ctx, "", prompts.FQDN, validate.FQDNValidator, profile.Network)
if err := validate.SeerFQDN(ctx.Context, profile.Network); err != nil {
return err
}

} else if profile.NetworkType == common.DreamlandNetwork {
if !slices.Contains(profile.History, profile.Network) {
profile.History = append(profile.History, profile.Network)
}

} else if network == common.DreamlandNetwork {
universes, err := dreamClient.Status()
if err != nil {
return fmt.Errorf("calling dreamland status failed with: %w", err)
Expand All @@ -104,6 +115,9 @@ func _select(ctx *cli.Context) error {
if err != nil {
return fmt.Errorf("universe selection failed with: %w", err)
}
} else {
profile.NetworkType = common.RemoteNetwork
profile.Network = network
}
}

Expand Down
3 changes: 2 additions & 1 deletion lib/login/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func Select(ctx *cli.Context, name string, setDefault bool) error {
return err
}

env.SetSelectedNetwork(ctx, profile.Network)
env.SetSelectedNetwork(ctx, profile.NetworkType)
env.SetNetworkUrl(ctx, profile.Network)
return env.SetSelectedUser(ctx, name)
}
1 change: 1 addition & 0 deletions singletons/config/profiles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func TestProfiles(t *testing.T) {
git_username: ""
git_email: ""
network: ""
history: []
`

configData, err := readConfig()
Expand Down
9 changes: 5 additions & 4 deletions singletons/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ type Profile struct {

// TODO get from config when verifying token
// may need to fake in tests
GitUsername string `yaml:"git_username"`
GitEmail string `yaml:"git_email"`
NetworkType string `yaml:"type,omitempty"`
Network string `yaml:"network"`
GitUsername string `yaml:"git_username"`
GitEmail string `yaml:"git_email"`
NetworkType string `yaml:"type,omitempty"`
Network string `yaml:"network"`
History []string `yaml:"history"`
}

type Project struct {
Expand Down

0 comments on commit 57c081e

Please sign in to comment.