Skip to content

Commit 09684be

Browse files
authored
Add stackit git commands: list, describe, create and delete. (#725)
* Add stackit git commands: list, describe, create and delete. * Add stackit git list instances command * Add stackit git list instances command * Git. Move list package and add describe command * Stackit Git create instance command * Add delete command to the stackit git cli * Stop using new variables for the globalflags.ProjectIdFlag and Docs generation * Stop using new variables for the globalflags.ProjectIdFlag and minor issues * Add docs of the stackit git instance command * Stackit git: add vertical table on describe and limit list command * Delete toolchain opetion from go.mod file * Get go.mod and go.sum from main
1 parent 1bb3c33 commit 09684be

22 files changed

+1895
-0
lines changed

docs/stackit.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ stackit [flags]
3333
* [stackit config](./stackit_config.md) - Provides functionality for CLI configuration options
3434
* [stackit curl](./stackit_curl.md) - Executes an authenticated HTTP request to an endpoint
3535
* [stackit dns](./stackit_dns.md) - Provides functionality for DNS
36+
* [stackit git](./stackit_git.md) - Provides functionality for STACKIT Git
3637
* [stackit image](./stackit_image.md) - Manage server images
3738
* [stackit key-pair](./stackit_key-pair.md) - Provides functionality for SSH key pairs
3839
* [stackit load-balancer](./stackit_load-balancer.md) - Provides functionality for Load Balancer

docs/stackit_git.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
## stackit git
2+
3+
Provides functionality for STACKIT Git
4+
5+
### Synopsis
6+
7+
Provides functionality for STACKIT Git.
8+
9+
```
10+
stackit git [flags]
11+
```
12+
13+
### Options
14+
15+
```
16+
-h, --help Help for "stackit git"
17+
```
18+
19+
### Options inherited from parent commands
20+
21+
```
22+
-y, --assume-yes If set, skips all confirmation prompts
23+
--async If set, runs the command asynchronously
24+
-o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"]
25+
-p, --project-id string Project ID
26+
--region string Target region for region-specific requests
27+
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
28+
```
29+
30+
### SEE ALSO
31+
32+
* [stackit](./stackit.md) - Manage STACKIT resources using the command line
33+
* [stackit git create](./stackit_git_create.md) - Creates STACKIT Git instance
34+
* [stackit git delete](./stackit_git_delete.md) - Deletes STACKIT Git instance
35+
* [stackit git describe](./stackit_git_describe.md) - Describes STACKIT Git instance
36+
* [stackit git list](./stackit_git_list.md) - Lists all instances of STACKIT Git.
37+

docs/stackit_git_create.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
## stackit git create
2+
3+
Creates STACKIT Git instance
4+
5+
### Synopsis
6+
7+
Create a STACKIT Git instance by name.
8+
9+
```
10+
stackit git create [flags]
11+
```
12+
13+
### Examples
14+
15+
```
16+
Create a instance with name 'my-new-instance'
17+
$ stackit git create --name my-new-instance
18+
```
19+
20+
### Options
21+
22+
```
23+
-h, --help Help for "stackit git create"
24+
--name string The name of the instance.
25+
```
26+
27+
### Options inherited from parent commands
28+
29+
```
30+
-y, --assume-yes If set, skips all confirmation prompts
31+
--async If set, runs the command asynchronously
32+
-o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"]
33+
-p, --project-id string Project ID
34+
--region string Target region for region-specific requests
35+
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
36+
```
37+
38+
### SEE ALSO
39+
40+
* [stackit git](./stackit_git.md) - Provides functionality for STACKIT Git
41+

docs/stackit_git_delete.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
## stackit git delete
2+
3+
Deletes STACKIT Git instance
4+
5+
### Synopsis
6+
7+
Deletes a STACKIT Git instance by its internal ID.
8+
9+
```
10+
stackit git delete INSTANCE_ID [flags]
11+
```
12+
13+
### Examples
14+
15+
```
16+
Delete a instance with ID "xxx"
17+
$ stackit git delete xxx
18+
```
19+
20+
### Options
21+
22+
```
23+
-h, --help Help for "stackit git delete"
24+
```
25+
26+
### Options inherited from parent commands
27+
28+
```
29+
-y, --assume-yes If set, skips all confirmation prompts
30+
--async If set, runs the command asynchronously
31+
-o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"]
32+
-p, --project-id string Project ID
33+
--region string Target region for region-specific requests
34+
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
35+
```
36+
37+
### SEE ALSO
38+
39+
* [stackit git](./stackit_git.md) - Provides functionality for STACKIT Git
40+

docs/stackit_git_describe.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
## stackit git describe
2+
3+
Describes STACKIT Git instance
4+
5+
### Synopsis
6+
7+
Describes a STACKIT Git instance by its internal ID.
8+
9+
```
10+
stackit git describe INSTANCE_ID [flags]
11+
```
12+
13+
### Examples
14+
15+
```
16+
Describe instance "xxx"
17+
$ stackit git describe xxx
18+
```
19+
20+
### Options
21+
22+
```
23+
-h, --help Help for "stackit git describe"
24+
```
25+
26+
### Options inherited from parent commands
27+
28+
```
29+
-y, --assume-yes If set, skips all confirmation prompts
30+
--async If set, runs the command asynchronously
31+
-o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"]
32+
-p, --project-id string Project ID
33+
--region string Target region for region-specific requests
34+
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
35+
```
36+
37+
### SEE ALSO
38+
39+
* [stackit git](./stackit_git.md) - Provides functionality for STACKIT Git
40+

docs/stackit_git_list.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
## stackit git list
2+
3+
Lists all instances of STACKIT Git.
4+
5+
### Synopsis
6+
7+
Lists all instances of STACKIT Git for the current project.
8+
9+
```
10+
stackit git list [flags]
11+
```
12+
13+
### Examples
14+
15+
```
16+
List all STACKIT Git instances
17+
$ stackit git instance list
18+
19+
Lists up to 10 STACKIT Git instances
20+
$ stackit git instance list --limit=10
21+
```
22+
23+
### Options
24+
25+
```
26+
-h, --help Help for "stackit git list"
27+
--limit int Limit the output to the first n elements
28+
```
29+
30+
### Options inherited from parent commands
31+
32+
```
33+
-y, --assume-yes If set, skips all confirmation prompts
34+
--async If set, runs the command asynchronously
35+
-o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"]
36+
-p, --project-id string Project ID
37+
--region string Target region for region-specific requests
38+
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
39+
```
40+
41+
### SEE ALSO
42+
43+
* [stackit git](./stackit_git.md) - Provides functionality for STACKIT Git
44+

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ require (
1919
github.com/stackitcloud/stackit-sdk-go/services/alb v0.2.3
2020
github.com/stackitcloud/stackit-sdk-go/services/authorization v0.6.3
2121
github.com/stackitcloud/stackit-sdk-go/services/dns v0.13.3
22+
github.com/stackitcloud/stackit-sdk-go/services/git v0.3.3
2223
github.com/stackitcloud/stackit-sdk-go/services/iaas v0.22.2
2324
github.com/stackitcloud/stackit-sdk-go/services/mongodbflex v1.0.1
2425
github.com/stackitcloud/stackit-sdk-go/services/opensearch v0.21.2

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,8 @@ github.com/stackitcloud/stackit-sdk-go/services/authorization v0.6.3 h1:aXVMNdiH
570570
github.com/stackitcloud/stackit-sdk-go/services/authorization v0.6.3/go.mod h1:dJ19ZwFjp2bfC5ZobXV3vUdSpE3quUw3GuoFSKLpHIo=
571571
github.com/stackitcloud/stackit-sdk-go/services/dns v0.13.3 h1:WJnA6YTOlBxMJf9PRuxkXduxj+fyWi+wOks69PvN1qI=
572572
github.com/stackitcloud/stackit-sdk-go/services/dns v0.13.3/go.mod h1:PMHoavoIaRZpkI9BA0nsnRjGoHASVSBon45XB3QyhMA=
573+
github.com/stackitcloud/stackit-sdk-go/services/git v0.3.3 h1:/NXxiJf/NFpj+DFXc07vsx+0he/xufdhIskXUlSeyyw=
574+
github.com/stackitcloud/stackit-sdk-go/services/git v0.3.3/go.mod h1:XhXHJpOVC9Rpwyf1G+EpMbprBafH9aZb8vWBdR+z0WM=
573575
github.com/stackitcloud/stackit-sdk-go/services/iaas v0.22.2 h1:zh6e2eHgqex++fr6N0RK7wMMBGesC3QhBd42FdTq2Z8=
574576
github.com/stackitcloud/stackit-sdk-go/services/iaas v0.22.2/go.mod h1:QNH50Pq0Hu21lLDOwa02PIjRjTl0LfEdHoz5snGQRn8=
575577
github.com/stackitcloud/stackit-sdk-go/services/loadbalancer v1.0.3 h1:StQg5p4h1aLFwIHQWWe5l44aFue+ZvQMGDbypP1DMYw=

internal/cmd/git/create/create.go

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
package create
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
8+
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
9+
10+
"github.com/goccy/go-yaml"
11+
"github.com/spf13/cobra"
12+
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
13+
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
14+
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
15+
"github.com/stackitcloud/stackit-cli/internal/pkg/flags"
16+
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
17+
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
18+
"github.com/stackitcloud/stackit-cli/internal/pkg/services/git/client"
19+
"github.com/stackitcloud/stackit-cli/internal/pkg/spinner"
20+
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
21+
"github.com/stackitcloud/stackit-sdk-go/services/git"
22+
"github.com/stackitcloud/stackit-sdk-go/services/git/wait"
23+
)
24+
25+
const (
26+
nameFlag = "name"
27+
)
28+
29+
type inputModel struct {
30+
*globalflags.GlobalFlagModel
31+
Id *string
32+
Name string
33+
}
34+
35+
func NewCmd(params *params.CmdParams) *cobra.Command {
36+
cmd := &cobra.Command{
37+
Use: "create",
38+
Short: "Creates STACKIT Git instance",
39+
Long: "Create a STACKIT Git instance by name.",
40+
Args: args.NoArgs,
41+
Example: examples.Build(
42+
examples.NewExample(
43+
`Create a instance with name 'my-new-instance'`,
44+
`$ stackit git create --name my-new-instance`,
45+
),
46+
),
47+
RunE: func(cmd *cobra.Command, _ []string) (err error) {
48+
ctx := context.Background()
49+
model, err := parseInput(params.Printer, cmd)
50+
if err != nil {
51+
return err
52+
}
53+
54+
// Configure API client
55+
apiClient, err := client.ConfigureClient(params.Printer)
56+
if err != nil {
57+
return err
58+
}
59+
60+
if !model.AssumeYes {
61+
prompt := fmt.Sprintf("Are you sure you want to create the instance %q?", model.Name)
62+
err = params.Printer.PromptForConfirmation(prompt)
63+
if err != nil {
64+
return err
65+
}
66+
}
67+
68+
// Call API
69+
request := buildRequest(ctx, model, apiClient)
70+
71+
result, err := request.Execute()
72+
if err != nil {
73+
return fmt.Errorf("create stackit git instance: %w", err)
74+
}
75+
model.Id = result.Id
76+
77+
// Wait for async operation, if async mode not enabled
78+
if !model.Async {
79+
s := spinner.New(params.Printer)
80+
s.Start("Creating stackit git instance")
81+
_, err = wait.CreateGitInstanceWaitHandler(ctx, apiClient, model.ProjectId, *model.Id).WaitWithContext(ctx)
82+
if err != nil {
83+
return fmt.Errorf("wait for stackit git Instance creation: %w", err)
84+
}
85+
s.Stop()
86+
}
87+
88+
return outputResult(params.Printer, model, result)
89+
},
90+
}
91+
92+
configureFlags(cmd)
93+
return cmd
94+
}
95+
96+
func configureFlags(cmd *cobra.Command) {
97+
cmd.Flags().String(nameFlag, "", "The name of the instance.")
98+
if err := flags.MarkFlagsRequired(cmd, nameFlag); err != nil {
99+
cobra.CheckErr(err)
100+
}
101+
}
102+
103+
func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
104+
globalFlags := globalflags.Parse(p, cmd)
105+
106+
if globalFlags.ProjectId == "" {
107+
return nil, &errors.ProjectIdError{}
108+
}
109+
name := flags.FlagToStringValue(p, cmd, nameFlag)
110+
111+
model := inputModel{
112+
GlobalFlagModel: globalFlags,
113+
Name: name,
114+
}
115+
116+
if p.IsVerbosityDebug() {
117+
modelStr, err := print.BuildDebugStrFromInputModel(model)
118+
if err != nil {
119+
p.Debug(print.ErrorLevel, "convert model to string for debugging: %v", err)
120+
} else {
121+
p.Debug(print.DebugLevel, "parsed input values: %s", modelStr)
122+
}
123+
}
124+
125+
return &model, nil
126+
}
127+
128+
func buildRequest(ctx context.Context, model *inputModel, apiClient *git.APIClient) git.ApiCreateInstanceRequest {
129+
return apiClient.CreateInstance(ctx, model.ProjectId).CreateInstancePayload(createPayload(model))
130+
}
131+
132+
func createPayload(model *inputModel) git.CreateInstancePayload {
133+
return git.CreateInstancePayload{
134+
Name: &model.Name,
135+
}
136+
}
137+
138+
func outputResult(p *print.Printer, model *inputModel, resp *git.Instance) error {
139+
if model == nil {
140+
return fmt.Errorf("input model is nil")
141+
}
142+
var outputFormat string
143+
if model.GlobalFlagModel != nil {
144+
outputFormat = model.OutputFormat
145+
}
146+
switch outputFormat {
147+
case print.JSONOutputFormat:
148+
details, err := json.MarshalIndent(resp, "", " ")
149+
if err != nil {
150+
return fmt.Errorf("marshal instance: %w", err)
151+
}
152+
p.Outputln(string(details))
153+
154+
return nil
155+
case print.YAMLOutputFormat:
156+
details, err := yaml.MarshalWithOptions(resp, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
157+
if err != nil {
158+
return fmt.Errorf("marshal iminstanceage: %w", err)
159+
}
160+
p.Outputln(string(details))
161+
162+
return nil
163+
default:
164+
p.Outputf("Created instance %q with id %s\n", model.Name, utils.PtrString(model.Id))
165+
return nil
166+
}
167+
}

0 commit comments

Comments
 (0)