1616package cmd
1717
1818import (
19+ "fmt"
20+
21+ "github.com/chainloop-dev/chainloop/app/cli/internal/action"
1922 "github.com/spf13/cobra"
2023)
2124
@@ -25,7 +28,7 @@ func newCASBackendCmd() *cobra.Command {
2528 Short : "Operations on Artifact CAS backends" ,
2629 }
2730
28- cmd .AddCommand (newCASBackendListCmd (), newCASBackendAddCmd (), newCASBackendUpdateCmd ())
31+ cmd .AddCommand (newCASBackendListCmd (), newCASBackendAddCmd (), newCASBackendUpdateCmd (), newCASBackendDeleteCmd () )
2932 return cmd
3033}
3134
@@ -54,3 +57,61 @@ func newCASBackendUpdateCmd() *cobra.Command {
5457 cmd .AddCommand (newCASBackendUpdateOCICmd ())
5558 return cmd
5659}
60+
61+ // confirmDefaultCASBackendOverride asks the user to confirm the override of the default CAS backend
62+ // in the event that there is one already set and its not the same as the one we are setting
63+ func confirmDefaultCASBackendOverride (actionOpts * action.ActionsOpts , id string ) (bool , error ) {
64+ // get existing backends
65+ backends , err := action .NewCASBackendList (actionOpts ).Run ()
66+ if err != nil {
67+ return false , fmt .Errorf ("failed to list existing CAS backends: %w" , err )
68+ }
69+
70+ // Find the default
71+ var defaultB * action.CASBackendItem
72+ for _ , b := range backends {
73+ if b .Default {
74+ defaultB = b
75+ break
76+ }
77+ }
78+
79+ // If there is none or there is but it's the same as the one we are setting, we are ok
80+ if defaultB == nil || (id != "" && id == defaultB .ID ) {
81+ return true , nil
82+ }
83+
84+ // Ask the user to confirm the override
85+ return confirmationPrompt ("You are changing the default CAS backend in your organization" ), nil
86+ }
87+
88+ // If we are removing the default we confirm too
89+ func confirmDefaultCASBackendRemoval (actionOpts * action.ActionsOpts , id string ) (bool , error ) {
90+ return confirmDefaultCASBackendUnset (id , "You are deleting the default CAS backend." , actionOpts )
91+ }
92+
93+ func confirmDefaultCASBackendUnset (id , msg string , actionOpts * action.ActionsOpts ) (bool , error ) {
94+ // get existing backends
95+ backends , err := action .NewCASBackendList (actionOpts ).Run ()
96+ if err != nil {
97+ return false , fmt .Errorf ("failed to list existing CAS backends: %w" , err )
98+ }
99+
100+ for _ , b := range backends {
101+ // We are removing ourselves as the default, ask the user to confirm
102+ if b .Default && b .ID == id {
103+ return confirmationPrompt (msg ), nil
104+ }
105+ }
106+
107+ return true , nil
108+ }
109+
110+ // y/n confirmation prompt
111+ func confirmationPrompt (msg string ) bool {
112+ fmt .Printf ("%s\n Please confirm to continue y/N\n " , msg )
113+ var gotChallenge string
114+ fmt .Scanln (& gotChallenge )
115+
116+ return gotChallenge == "y" || gotChallenge == "Y"
117+ }
0 commit comments