Skip to content

Commit e9bfe4d

Browse files
mihaichiticalexo-bunnyshell
authored andcommitted
[ND-7649][ND-7648] - implement env abort
1 parent 09e5647 commit e9bfe4d

3 files changed

Lines changed: 106 additions & 2 deletions

File tree

cmd/environment/action/abort.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package action
2+
3+
import (
4+
"net/http"
5+
6+
"bunnyshell.com/cli/pkg/api/environment"
7+
"bunnyshell.com/cli/pkg/config"
8+
"bunnyshell.com/cli/pkg/lib"
9+
"github.com/spf13/cobra"
10+
)
11+
12+
func init() {
13+
options := config.GetOptions()
14+
settings := config.GetSettings()
15+
16+
abortOptions := environment.NewAbortOptions("")
17+
18+
command := &cobra.Command{
19+
Use: "abort",
20+
21+
ValidArgsFunction: cobra.NoFileCompletions,
22+
23+
RunE: func(cmd *cobra.Command, args []string) error {
24+
abortOptions.ID = settings.Profile.Context.Environment
25+
26+
event, err := environment.Abort(abortOptions)
27+
if err != nil {
28+
return lib.FormatCommandError(cmd, err)
29+
}
30+
31+
if event != nil {
32+
return lib.FormatCommandData(cmd, event)
33+
}
34+
35+
if settings.IsStylish() {
36+
cmd.Println("Nothing to abort")
37+
return nil
38+
}
39+
40+
return lib.FormatCommandData(cmd, map[string]interface{}{
41+
"status": http.StatusNoContent,
42+
"detail": "Nothing to abort",
43+
})
44+
},
45+
}
46+
47+
flags := command.Flags()
48+
flags.AddFlag(options.Environment.GetRequiredFlag("id"))
49+
50+
mainCmd.AddCommand(command)
51+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package environment
2+
3+
import (
4+
"net/http"
5+
6+
"bunnyshell.com/cli/pkg/api"
7+
"bunnyshell.com/cli/pkg/api/common"
8+
"bunnyshell.com/cli/pkg/lib"
9+
"bunnyshell.com/sdk"
10+
)
11+
12+
type AbortOptions struct {
13+
common.ItemOptions
14+
}
15+
16+
func NewAbortOptions(id string) *AbortOptions {
17+
return &AbortOptions{
18+
ItemOptions: *common.NewItemOptions(id),
19+
}
20+
}
21+
22+
func Abort(options *AbortOptions) (*sdk.EventItem, error) {
23+
model, resp, err := AbortRaw(options)
24+
if resp != nil && resp.StatusCode == http.StatusNoContent {
25+
return nil, nil
26+
}
27+
28+
if err != nil {
29+
return nil, api.ParseError(resp, err)
30+
}
31+
32+
return model, nil
33+
}
34+
35+
func AbortRaw(options *AbortOptions) (*sdk.EventItem, *http.Response, error) {
36+
profile := options.GetProfile()
37+
38+
ctx, cancel := lib.GetContextFromProfile(profile)
39+
defer cancel()
40+
41+
request := lib.GetAPIFromProfile(profile).EnvironmentAPI.EnvironmentAbort(ctx, options.ID)
42+
43+
return request.Execute()
44+
}

pkg/formatter/stylish.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"fmt"
66
"text/tabwriter"
7+
"time"
78

89
"bunnyshell.com/cli/pkg/api"
910
"bunnyshell.com/sdk"
@@ -323,8 +324,8 @@ func tabulateEventItem(w *tabwriter.Writer, item *sdk.EventItem) {
323324
fmt.Fprintf(w, "%v\t %v\n", "OrganizationID", item.GetOrganization())
324325
fmt.Fprintf(w, "%v\t %v\n", "Status", item.GetStatus())
325326
fmt.Fprintf(w, "%v\t %v\n", "Type", item.GetType())
326-
fmt.Fprintf(w, "%v\t %v\n", "CreatedAt", item.GetCreatedAt())
327-
fmt.Fprintf(w, "%v\t %v\n", "UpdatedAt", item.GetUpdatedAt())
327+
fmt.Fprintf(w, "%v\t %v\n", "CreatedAt", formatStylishTimestamp(item.GetCreatedAt()))
328+
fmt.Fprintf(w, "%v\t %v\n", "UpdatedAt", formatStylishTimestamp(item.GetUpdatedAt()))
328329
}
329330

330331
func tabulateEnvironmentVariableItem(w *tabwriter.Writer, item *sdk.EnvironmentVariableItem) {
@@ -380,3 +381,11 @@ func writeJSON(writer *tabwriter.Writer, data any) error {
380381

381382
return err
382383
}
384+
385+
func formatStylishTimestamp(value time.Time) string {
386+
if value.IsZero() {
387+
return ""
388+
}
389+
390+
return value.Format(time.RFC3339)
391+
}

0 commit comments

Comments
 (0)