Skip to content
Open
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
2 changes: 2 additions & 0 deletions cmd/tscli/config/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/jaxxstorm/tscli/cmd/tscli/config/get"
"github.com/jaxxstorm/tscli/cmd/tscli/config/set"
"github.com/jaxxstorm/tscli/cmd/tscli/config/show"
"github.com/jaxxstorm/tscli/cmd/tscli/config/tailnet"
"github.com/spf13/cobra"
)

Expand All @@ -17,5 +18,6 @@ func Command() *cobra.Command {
command.AddCommand(show.Command())
command.AddCommand(set.Command())
command.AddCommand(get.Command())
command.AddCommand(tailnet.Command())
return command
}
34 changes: 34 additions & 0 deletions cmd/tscli/config/tailnet/add/cli.go
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
},
}
}
24 changes: 24 additions & 0 deletions cmd/tscli/config/tailnet/cli.go
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
}
86 changes: 86 additions & 0 deletions cmd/tscli/config/tailnet/list/cli.go
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
},
}
}
31 changes: 31 additions & 0 deletions cmd/tscli/config/tailnet/remove/cli.go
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
},
}
}
31 changes: 31 additions & 0 deletions cmd/tscli/config/tailnet/switch_/cli.go
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
},
}
}
67 changes: 67 additions & 0 deletions cmd/tscli/config/tailnet/update/cli.go
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
Copy link
Contributor

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.

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
},
}
}
19 changes: 16 additions & 3 deletions cmd/tscli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,28 @@ func configureCLI() *cobra.Command {
Use: "tscli",
Long: "A CLI tool for interacting with the Tailscale API.",
PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
// skip validation for "help" and "version" commands
if cmd.Name() == "help" || cmd.Name() == "version" {
// skip validation for "help", "version", and "config" commands
if cmd.Name() == "help" || cmd.Name() == "version" || cmd.Name() == "config" ||
(cmd.Parent() != nil && cmd.Parent().Name() == "config") ||
(cmd.Parent() != nil && cmd.Parent().Parent() != nil && cmd.Parent().Parent().Name() == "config") {
return nil
}

_ = v.BindPFlags(cmd.Flags())
if v.GetString("api-key") == "" {

// Check for API key based on configuration mode
var hasAPIKey bool
if config.IsNewConfig() {
tailnetConfig, err := config.GetActiveTailnetConfig()
hasAPIKey = err == nil && tailnetConfig != nil && tailnetConfig.APIKey != ""
} else {
hasAPIKey = v.GetString("api-key") != ""
}

if !hasAPIKey {
return fmt.Errorf("a Tailscale API key is required")
}

if v.GetString("tailnet") == "" {
v.Set("tailnet", "-")
}
Expand Down
Loading