-
Notifications
You must be signed in to change notification settings - Fork 2
feat: add multi-tailnet configuration support to tscli #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jaxxstorm
wants to merge
1
commit into
main
Choose a base branch
from
config
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,34 @@ | ||
package add | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/jaxxstorm/tscli/pkg/config" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func Command() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "add <name> <api-key>", | ||
Short: "Add a new tailnet configuration", | ||
Args: cobra.ExactArgs(2), | ||
RunE: func(_ *cobra.Command, args []string) error { | ||
// Check if using legacy config and warn | ||
if config.IsLegacyConfig() { | ||
fmt.Println("Warning: You are currently using legacy configuration (single api-key).") | ||
fmt.Println("Adding a tailnet will switch you to the new multi-tailnet configuration.") | ||
fmt.Println("Your existing api-key will remain accessible via the legacy config until you migrate.") | ||
fmt.Println() | ||
} | ||
|
||
name, apiKey := args[0], args[1] | ||
|
||
if err := config.AddTailnet(name, apiKey); err != nil { | ||
return fmt.Errorf("failed to add tailnet: %w", err) | ||
} | ||
|
||
fmt.Printf("Tailnet %q added successfully\n", name) | ||
return nil | ||
}, | ||
} | ||
} |
This file contains hidden or 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,24 @@ | ||
package tailnet | ||
|
||
import ( | ||
"github.com/jaxxstorm/tscli/cmd/tscli/config/tailnet/add" | ||
"github.com/jaxxstorm/tscli/cmd/tscli/config/tailnet/list" | ||
"github.com/jaxxstorm/tscli/cmd/tscli/config/tailnet/remove" | ||
"github.com/jaxxstorm/tscli/cmd/tscli/config/tailnet/switch_" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func Command() *cobra.Command { | ||
command := &cobra.Command{ | ||
Use: "tailnet", | ||
Short: "Manage tailnet configurations", | ||
Long: "Commands to add, remove, list, and switch between tailnet configurations", | ||
} | ||
|
||
command.AddCommand(add.Command()) | ||
command.AddCommand(list.Command()) | ||
command.AddCommand(remove.Command()) | ||
command.AddCommand(switch_.Command()) | ||
|
||
return command | ||
} |
This file contains hidden or 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,86 @@ | ||
package list | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/jaxxstorm/tscli/pkg/config" | ||
"github.com/jaxxstorm/tscli/pkg/output" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
func Command() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "list", | ||
Short: "List all tailnet configurations", | ||
RunE: func(_ *cobra.Command, _ []string) error { | ||
// Check if using legacy config | ||
if config.IsLegacyConfig() { | ||
fmt.Println("Warning: You are using legacy configuration (single api-key).") | ||
fmt.Println("No tailnet configurations found. Use 'tscli config tailnet add' to start using multi-tailnet configuration.") | ||
return nil | ||
} | ||
|
||
tailnets, active, err := config.ListTailnets() | ||
if err != nil { | ||
return fmt.Errorf("failed to list tailnets: %w", err) | ||
} | ||
|
||
if len(tailnets) == 0 { | ||
fmt.Println("No tailnets configured") | ||
return nil | ||
} | ||
|
||
outputType := viper.GetString("output") | ||
if outputType == "json" || outputType == "yaml" { | ||
type tailnetDisplay struct { | ||
Name string `json:"name" yaml:"name"` | ||
APIKey string `json:"api-key" yaml:"api-key"` | ||
IsActive bool `json:"is-active" yaml:"is-active"` | ||
} | ||
|
||
var displayTailnets []tailnetDisplay | ||
for _, tailnet := range tailnets { | ||
displayTailnets = append(displayTailnets, tailnetDisplay{ | ||
Name: tailnet.Name, | ||
APIKey: tailnet.APIKey, | ||
IsActive: tailnet.Name == active, | ||
}) | ||
} | ||
|
||
out, err := json.MarshalIndent(displayTailnets, "", " ") | ||
if err != nil { | ||
return fmt.Errorf("failed to marshal output: %w", err) | ||
} | ||
|
||
return output.Print(outputType, out) | ||
} | ||
|
||
// Human-readable output | ||
fmt.Println("Configured tailnets:") | ||
for _, tailnet := range tailnets { | ||
marker := " " | ||
if tailnet.Name == active { | ||
marker = "*" | ||
} | ||
|
||
// Safely truncate API key | ||
apiKeyDisplay := tailnet.APIKey | ||
if len(apiKeyDisplay) > 8 { | ||
apiKeyDisplay = apiKeyDisplay[:8] + "..." | ||
} else if len(apiKeyDisplay) > 0 { | ||
apiKeyDisplay = apiKeyDisplay + "..." | ||
} | ||
|
||
fmt.Printf("%s %s (API Key: %s)\n", marker, tailnet.Name, apiKeyDisplay) | ||
} | ||
|
||
if active != "" { | ||
fmt.Printf("\nActive tailnet: %s\n", active) | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
} |
This file contains hidden or 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,31 @@ | ||
package remove | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/jaxxstorm/tscli/pkg/config" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func Command() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "remove <name>", | ||
Short: "Remove a tailnet configuration", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(_ *cobra.Command, args []string) error { | ||
// Check if using legacy config | ||
if config.IsLegacyConfig() { | ||
return fmt.Errorf("cannot remove tailnets: you are using legacy configuration (single api-key). Use 'tscli config tailnet add' to start using multi-tailnet configuration") | ||
} | ||
|
||
name := args[0] | ||
|
||
if err := config.RemoveTailnet(name); err != nil { | ||
return fmt.Errorf("failed to remove tailnet: %w", err) | ||
} | ||
|
||
fmt.Printf("Tailnet %q removed successfully\n", name) | ||
return nil | ||
}, | ||
} | ||
} |
This file contains hidden or 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,31 @@ | ||
package switch_ | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/jaxxstorm/tscli/pkg/config" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func Command() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "switch <name>", | ||
Short: "Switch to a different tailnet configuration", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(_ *cobra.Command, args []string) error { | ||
// Check if using legacy config | ||
if config.IsLegacyConfig() { | ||
return fmt.Errorf("cannot switch tailnets: you are using legacy configuration (single api-key). Use 'tscli config tailnet add' to start using multi-tailnet configuration") | ||
} | ||
|
||
name := args[0] | ||
|
||
if err := config.SetActiveTailnet(name); err != nil { | ||
return fmt.Errorf("failed to switch tailnet: %w", err) | ||
} | ||
|
||
fmt.Printf("Switched to tailnet %q\n", name) | ||
return nil | ||
}, | ||
} | ||
} |
This file contains hidden or 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,67 @@ | ||
package update | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/jaxxstorm/tscli/pkg/config" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func Command() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "update <name> <new-name> [new-api-key]", | ||
Short: "Update an existing tailnet configuration", | ||
Args: cobra.RangeArgs(2, 3), | ||
RunE: func(_ *cobra.Command, args []string) error { | ||
oldName := args[0] | ||
newName := args[1] | ||
var newAPIKey string | ||
|
||
// Get current tailnets | ||
tailnets, active, err := config.ListTailnets() | ||
if err != nil { | ||
return fmt.Errorf("failed to list tailnets: %w", err) | ||
} | ||
|
||
// Find the tailnet to update | ||
var currentTailnet *config.TailnetConfig | ||
for _, tailnet := range tailnets { | ||
if tailnet.Name == oldName { | ||
currentTailnet = &tailnet | ||
break | ||
} | ||
} | ||
|
||
if currentTailnet == nil { | ||
return fmt.Errorf("tailnet %q not found", oldName) | ||
} | ||
|
||
// Use existing API key if not provided | ||
if len(args) == 3 { | ||
newAPIKey = args[2] | ||
} else { | ||
newAPIKey = currentTailnet.APIKey | ||
} | ||
|
||
// Remove old tailnet | ||
if err := config.RemoveTailnet(oldName); err != nil { | ||
return fmt.Errorf("failed to remove old tailnet: %w", err) | ||
} | ||
|
||
// Add new tailnet | ||
if err := config.AddTailnet(newName, newAPIKey); err != nil { | ||
return fmt.Errorf("failed to add updated tailnet: %w", err) | ||
} | ||
|
||
// If this was the active tailnet, make the new one active | ||
if active == oldName { | ||
if err := config.SetActiveTailnet(newName); err != nil { | ||
return fmt.Errorf("failed to set active tailnet: %w", err) | ||
} | ||
} | ||
|
||
fmt.Printf("Tailnet updated from %q to %q\n", oldName, newName) | ||
return nil | ||
}, | ||
} | ||
} |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid taking the address of the loop variable when searching for the existing tailnet; iterate by index to safely get a pointer.