Skip to content
Merged
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 internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
configcmd "github.com/enthus-appdev/n8n-cli/internal/cmd/config"
executioncmd "github.com/enthus-appdev/n8n-cli/internal/cmd/execution"
projectcmd "github.com/enthus-appdev/n8n-cli/internal/cmd/project"
variablecmd "github.com/enthus-appdev/n8n-cli/internal/cmd/variable"
workflowcmd "github.com/enthus-appdev/n8n-cli/internal/cmd/workflow"
)

Expand Down Expand Up @@ -40,6 +41,7 @@ func init() {
rootCmd.AddCommand(workflowcmd.NewWorkflowCmd())
rootCmd.AddCommand(executioncmd.NewExecutionCmd())
rootCmd.AddCommand(projectcmd.NewProjectCmd())
rootCmd.AddCommand(variablecmd.NewVariableCmd())
rootCmd.AddCommand(newVersionCmd())
}

Expand Down
220 changes: 220 additions & 0 deletions internal/cmd/variable/variable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
package variable

import (
"encoding/json"
"fmt"
"os"
"strings"

"github.com/spf13/cobra"

"github.com/enthus-appdev/n8n-cli/internal/api"
"github.com/enthus-appdev/n8n-cli/internal/config"
)

func NewVariableCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "variable",
Aliases: []string{"var"},
Short: "Manage n8n variables",
Long: `List, create, update, and delete n8n environment variables.`,
}

cmd.AddCommand(newListCmd())
cmd.AddCommand(newGetCmd())
cmd.AddCommand(newCreateCmd())
cmd.AddCommand(newUpdateCmd())
cmd.AddCommand(newDeleteCmd())

return cmd
}

func getClient() (*api.Client, error) {
cfg, err := config.Load()
if err != nil {
return nil, fmt.Errorf("not configured. Run 'n8nctl config init' first")
}

instance, err := cfg.GetCurrentInstance()
if err != nil {
return nil, err
}

return api.NewClient(instance.URL, instance.APIKey), nil
}

func newListCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "List all variables",
RunE: func(cmd *cobra.Command, args []string) error {
client, err := getClient()
if err != nil {
return err
}

vars, err := client.ListVariables(0, "")
if err != nil {
return fmt.Errorf("failed to list variables: %w", err)
}

jsonFlag, _ := cmd.Flags().GetBool("json")
if jsonFlag {
return printJSON(vars)
}

if len(vars) == 0 {
fmt.Println("No variables found.")
return nil
}

fmt.Printf("%-8s %-40s %s\n", "ID", "KEY", "VALUE")
fmt.Printf("%-8s %-40s %s\n", strings.Repeat("-", 8), strings.Repeat("-", 40), strings.Repeat("-", 50))
for _, v := range vars {
value := v.Value
if len(value) > 50 {
value = value[:47] + "..."
}
fmt.Printf("%-8s %-40s %s\n", v.ID, v.Key, value)
}

return nil
},
}

return cmd
}

func newGetCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "get <key>",
Short: "Get a variable by key",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
client, err := getClient()
if err != nil {
return err
}

vars, err := client.ListVariables(0, "")
if err != nil {
return fmt.Errorf("failed to list variables: %w", err)
}

key := args[0]
for _, v := range vars {
if v.Key == key {
jsonFlag, _ := cmd.Flags().GetBool("json")
if jsonFlag {
return printJSON(v)
}
fmt.Println(v.Value)
return nil
}
}

return fmt.Errorf("variable %q not found", key)
},
}

return cmd
}

func newCreateCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "create <key> <value>",
Short: "Create a new variable",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
client, err := getClient()
if err != nil {
return err
}

if err := client.CreateVariable(args[0], args[1]); err != nil {
return fmt.Errorf("failed to create variable: %w", err)
}

fmt.Printf("Created variable: %s\n", args[0])
return nil
},
}

return cmd
}

func newUpdateCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "update <key> <value>",
Short: "Update a variable by key",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
client, err := getClient()
if err != nil {
return err
}

// Resolve key to ID
vars, err := client.ListVariables(0, "")
if err != nil {
return fmt.Errorf("failed to list variables: %w", err)
}

key := args[0]
for _, v := range vars {
if v.Key == key {
if err := client.UpdateVariable(v.ID, key, args[1]); err != nil {
return fmt.Errorf("failed to update variable: %w", err)
}
fmt.Printf("Updated variable: %s\n", key)
return nil
}
}

return fmt.Errorf("variable %q not found", key)
},
}

return cmd
}

func newDeleteCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "delete <key>",
Short: "Delete a variable by key",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
client, err := getClient()
if err != nil {
return err
}

// Resolve key to ID
vars, err := client.ListVariables(0, "")
if err != nil {
return fmt.Errorf("failed to list variables: %w", err)
}

key := args[0]
for _, v := range vars {
if v.Key == key {
if err := client.DeleteVariable(v.ID); err != nil {
return fmt.Errorf("failed to delete variable: %w", err)
}
fmt.Printf("Deleted variable: %s\n", key)
return nil
}
}

return fmt.Errorf("variable %q not found", key)
},
}

return cmd
}

func printJSON(v interface{}) error {
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
return enc.Encode(v)
}