11package internal
22
33import (
4+ "fmt"
5+ "strconv"
46 "strings"
57
8+ "github.com/jahvon/tuikit/views"
69 "github.com/spf13/cobra"
710 "golang.org/x/exp/maps"
811
912 "github.com/jahvon/flow/cmd/internal/flags"
1013 "github.com/jahvon/flow/internal/context"
14+ "github.com/jahvon/flow/internal/filesystem"
15+ "github.com/jahvon/flow/internal/io"
1116 vaultIO "github.com/jahvon/flow/internal/io/vault"
1217 "github.com/jahvon/flow/internal/vault"
1318)
@@ -20,10 +25,10 @@ func RegisterVaultCmd(ctx *context.Context, rootCmd *cobra.Command) {
2025 Args : cobra .NoArgs ,
2126 }
2227 registerCreateVaultCmd (ctx , vaultCmd )
23- // registerGetVaultCmd(ctx, vaultCmd)
28+ registerGetVaultCmd (ctx , vaultCmd )
2429 registerListVaultCmd (ctx , vaultCmd )
2530 registerSwitchVaultCmd (ctx , vaultCmd )
26- // registerRemoveVaultCmd(ctx, vaultCmd)
31+ registerRemoveVaultCmd (ctx , vaultCmd )
2732 rootCmd .AddCommand (vaultCmd )
2833}
2934
@@ -76,6 +81,38 @@ func createVaultFunc(ctx *context.Context, cmd *cobra.Command, args []string) {
7681 }
7782}
7883
84+ func registerGetVaultCmd (ctx * context.Context , vaultCmd * cobra.Command ) {
85+ getCmd := & cobra.Command {
86+ Use : "get NAME" ,
87+ Aliases : []string {"view" , "show" },
88+ Short : "Get the details of a vault." ,
89+ Args : cobra .ExactArgs (1 ),
90+ ValidArgsFunction : func (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective ) {
91+ return maps .Keys (ctx .Config .Vaults ), cobra .ShellCompDirectiveNoFileComp
92+ },
93+ Run : func (cmd * cobra.Command , args []string ) { getVaultFunc (ctx , cmd , args ) },
94+ }
95+ RegisterFlag (ctx , getCmd , * flags .OutputFormatFlag )
96+ vaultCmd .AddCommand (getCmd )
97+ }
98+
99+ func getVaultFunc (ctx * context.Context , cmd * cobra.Command , args []string ) {
100+ logger := ctx .Logger
101+ outputFormat := flags .ValueFor [string ](ctx , cmd , * flags .OutputFormatFlag , false )
102+
103+ vaultName := args [0 ]
104+ if err := vault .ValidateReference (vaultName ); err != nil {
105+ logger .Fatalf ("invalid vault name '%s': %v" , vaultName , err )
106+ }
107+
108+ if TUIEnabled (ctx , cmd ) {
109+ view := vaultIO .NewVaultView (ctx .TUIContainer , vaultName )
110+ SetView (ctx , cmd , view )
111+ } else {
112+ vaultIO .PrintVault (logger , outputFormat , vaultName )
113+ }
114+ }
115+
79116func registerListVaultCmd (ctx * context.Context , vaultCmd * cobra.Command ) {
80117 listCmd := & cobra.Command {
81118 Use : "list" ,
@@ -105,13 +142,89 @@ func listVaultsFunc(ctx *context.Context, cmd *cobra.Command, _ []string) {
105142 }
106143}
107144
145+ func registerRemoveVaultCmd (ctx * context.Context , vaultCmd * cobra.Command ) {
146+ removeCmd := & cobra.Command {
147+ Use : "remove NAME" ,
148+ Aliases : []string {"rm" , "delete" },
149+ Short : "Remove an existing vault." ,
150+ Long : "Remove an existing vault by its name. The vault data will remain in it's original location, " +
151+ "but the vault will be unlinked from the global configuration.\n Note: You cannot remove the current vault." ,
152+ Args : cobra .ExactArgs (1 ),
153+ ValidArgsFunction : func (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective ) {
154+ return maps .Keys (ctx .Config .Vaults ), cobra .ShellCompDirectiveNoFileComp
155+ },
156+ Run : func (cmd * cobra.Command , args []string ) { removeVaultFunc (ctx , cmd , args ) },
157+ }
158+ vaultCmd .AddCommand (removeCmd )
159+ }
160+
161+ func removeVaultFunc (ctx * context.Context , _ * cobra.Command , args []string ) {
162+ logger := ctx .Logger
163+ vaultName := args [0 ]
164+
165+ form , err := views .NewForm (
166+ io .Theme (ctx .Config .Theme .String ()),
167+ ctx .StdIn (),
168+ ctx .StdOut (),
169+ & views.FormField {
170+ Key : "confirm" ,
171+ Type : views .PromptTypeConfirm ,
172+ Title : fmt .Sprintf ("Are you sure you want to remove the vault '%s'?" , vaultName ),
173+ })
174+ if err != nil {
175+ logger .FatalErr (err )
176+ }
177+ if err := form .Run (ctx .Ctx ); err != nil {
178+ logger .FatalErr (err )
179+ }
180+ resp := form .FindByKey ("confirm" ).Value ()
181+ if truthy , _ := strconv .ParseBool (resp ); ! truthy {
182+ logger .Warnf ("Aborting" )
183+ return
184+ }
185+
186+ userConfig := ctx .Config
187+ if userConfig .CurrentVault != nil && vaultName == * userConfig .CurrentVault {
188+ logger .Fatalf ("cannot remove the current vault" )
189+ }
190+ if _ , found := userConfig .Vaults [vaultName ]; ! found {
191+ logger .Fatalf ("vault %s was not found" , vaultName )
192+ }
193+
194+ delete (userConfig .Vaults , vaultName )
195+ if err := filesystem .WriteConfig (userConfig ); err != nil {
196+ logger .FatalErr (err )
197+ }
198+
199+ logger .Warnf ("Vault '%s' deleted" , vaultName )
200+
201+ }
202+
108203func registerSwitchVaultCmd (ctx * context.Context , vaultCmd * cobra.Command ) {
109204 switchCmd := & cobra.Command {
110205 Use : "switch NAME" ,
111206 Aliases : []string {"use" , "set" },
112207 Short : "Switch the active vault." ,
113208 Args : cobra .ExactArgs (1 ),
114- Run : func (cmd * cobra.Command , args []string ) { ctx .Logger .Fatalf ("not implemented yet" ) },
209+ ValidArgsFunction : func (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective ) {
210+ return maps .Keys (ctx .Config .Vaults ), cobra .ShellCompDirectiveNoFileComp
211+ },
212+ Run : func (cmd * cobra.Command , args []string ) { switchVaultFunc (ctx , cmd , args ) },
115213 }
116214 vaultCmd .AddCommand (switchCmd )
117215}
216+
217+ func switchVaultFunc (ctx * context.Context , _ * cobra.Command , args []string ) {
218+ logger := ctx .Logger
219+ vaultName := args [0 ]
220+ userConfig := ctx .Config
221+ if _ , found := userConfig .Vaults [vaultName ]; ! found {
222+ logger .Fatalf ("vault %s not found" , vaultName )
223+ }
224+ userConfig .CurrentVault = & vaultName
225+
226+ if err := filesystem .WriteConfig (userConfig ); err != nil {
227+ logger .FatalErr (err )
228+ }
229+ logger .PlainTextSuccess ("Vault set to " + vaultName )
230+ }
0 commit comments