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
1 change: 1 addition & 0 deletions docs/stackit.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ stackit [flags]
* [stackit config](./stackit_config.md) - Provides functionality for CLI configuration options
* [stackit curl](./stackit_curl.md) - Executes an authenticated HTTP request to an endpoint
* [stackit dns](./stackit_dns.md) - Provides functionality for DNS
* [stackit git](./stackit_git.md) - Provides functionality for STACKIT Git
* [stackit image](./stackit_image.md) - Manage server images
* [stackit key-pair](./stackit_key-pair.md) - Provides functionality for SSH key pairs
* [stackit load-balancer](./stackit_load-balancer.md) - Provides functionality for Load Balancer
Expand Down
37 changes: 37 additions & 0 deletions docs/stackit_git.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
## stackit git

Provides functionality for STACKIT Git

### Synopsis

Provides functionality for STACKIT Git.

```
stackit git [flags]
```

### Options

```
-h, --help Help for "stackit git"
```

### Options inherited from parent commands

```
-y, --assume-yes If set, skips all confirmation prompts
--async If set, runs the command asynchronously
-o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"]
-p, --project-id string Project ID
--region string Target region for region-specific requests
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
```

### SEE ALSO

* [stackit](./stackit.md) - Manage STACKIT resources using the command line
* [stackit git create](./stackit_git_create.md) - Creates STACKIT Git instance
* [stackit git delete](./stackit_git_delete.md) - Deletes STACKIT Git instance
* [stackit git describe](./stackit_git_describe.md) - Describes STACKIT Git instance
* [stackit git list](./stackit_git_list.md) - Lists all instances of STACKIT Git.

41 changes: 41 additions & 0 deletions docs/stackit_git_create.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
## stackit git create

Creates STACKIT Git instance

### Synopsis

Create a STACKIT Git instance by name.

```
stackit git create [flags]
```

### Examples

```
Create a instance with name 'my-new-instance'
$ stackit git create --name my-new-instance
```

### Options

```
-h, --help Help for "stackit git create"
--name string The name of the instance.
```

### Options inherited from parent commands

```
-y, --assume-yes If set, skips all confirmation prompts
--async If set, runs the command asynchronously
-o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"]
-p, --project-id string Project ID
--region string Target region for region-specific requests
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
```

### SEE ALSO

* [stackit git](./stackit_git.md) - Provides functionality for STACKIT Git

40 changes: 40 additions & 0 deletions docs/stackit_git_delete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
## stackit git delete

Deletes STACKIT Git instance

### Synopsis

Deletes a STACKIT Git instance by its internal ID.

```
stackit git delete INSTANCE_ID [flags]
```

### Examples

```
Delete a instance with ID "xxx"
$ stackit git delete xxx
```

### Options

```
-h, --help Help for "stackit git delete"
```

### Options inherited from parent commands

```
-y, --assume-yes If set, skips all confirmation prompts
--async If set, runs the command asynchronously
-o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"]
-p, --project-id string Project ID
--region string Target region for region-specific requests
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
```

### SEE ALSO

* [stackit git](./stackit_git.md) - Provides functionality for STACKIT Git

40 changes: 40 additions & 0 deletions docs/stackit_git_describe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
## stackit git describe

Describes STACKIT Git instance

### Synopsis

Describes a STACKIT Git instance by its internal ID.

```
stackit git describe INSTANCE_ID [flags]
```

### Examples

```
Describe instance "xxx"
$ stackit git describe xxx
```

### Options

```
-h, --help Help for "stackit git describe"
```

### Options inherited from parent commands

```
-y, --assume-yes If set, skips all confirmation prompts
--async If set, runs the command asynchronously
-o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"]
-p, --project-id string Project ID
--region string Target region for region-specific requests
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
```

### SEE ALSO

* [stackit git](./stackit_git.md) - Provides functionality for STACKIT Git

44 changes: 44 additions & 0 deletions docs/stackit_git_list.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
## stackit git list

Lists all instances of STACKIT Git.

### Synopsis

Lists all instances of STACKIT Git for the current project.

```
stackit git list [flags]
```

### Examples

```
List all STACKIT Git instances
$ stackit git instance list

Lists up to 10 STACKIT Git instances
$ stackit git instance list --limit=10
```

### Options

```
-h, --help Help for "stackit git list"
--limit int Limit the output to the first n elements
```

### Options inherited from parent commands

```
-y, --assume-yes If set, skips all confirmation prompts
--async If set, runs the command asynchronously
-o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"]
-p, --project-id string Project ID
--region string Target region for region-specific requests
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
```

### SEE ALSO

* [stackit git](./stackit_git.md) - Provides functionality for STACKIT Git

1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
github.com/stackitcloud/stackit-sdk-go/services/alb v0.2.3
github.com/stackitcloud/stackit-sdk-go/services/authorization v0.6.3
github.com/stackitcloud/stackit-sdk-go/services/dns v0.13.3
github.com/stackitcloud/stackit-sdk-go/services/git v0.3.3
github.com/stackitcloud/stackit-sdk-go/services/iaas v0.22.2
github.com/stackitcloud/stackit-sdk-go/services/mongodbflex v1.0.1
github.com/stackitcloud/stackit-sdk-go/services/opensearch v0.21.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,8 @@ github.com/stackitcloud/stackit-sdk-go/services/authorization v0.6.3 h1:aXVMNdiH
github.com/stackitcloud/stackit-sdk-go/services/authorization v0.6.3/go.mod h1:dJ19ZwFjp2bfC5ZobXV3vUdSpE3quUw3GuoFSKLpHIo=
github.com/stackitcloud/stackit-sdk-go/services/dns v0.13.3 h1:WJnA6YTOlBxMJf9PRuxkXduxj+fyWi+wOks69PvN1qI=
github.com/stackitcloud/stackit-sdk-go/services/dns v0.13.3/go.mod h1:PMHoavoIaRZpkI9BA0nsnRjGoHASVSBon45XB3QyhMA=
github.com/stackitcloud/stackit-sdk-go/services/git v0.3.3 h1:/NXxiJf/NFpj+DFXc07vsx+0he/xufdhIskXUlSeyyw=
github.com/stackitcloud/stackit-sdk-go/services/git v0.3.3/go.mod h1:XhXHJpOVC9Rpwyf1G+EpMbprBafH9aZb8vWBdR+z0WM=
github.com/stackitcloud/stackit-sdk-go/services/iaas v0.22.2 h1:zh6e2eHgqex++fr6N0RK7wMMBGesC3QhBd42FdTq2Z8=
github.com/stackitcloud/stackit-sdk-go/services/iaas v0.22.2/go.mod h1:QNH50Pq0Hu21lLDOwa02PIjRjTl0LfEdHoz5snGQRn8=
github.com/stackitcloud/stackit-sdk-go/services/loadbalancer v1.0.3 h1:StQg5p4h1aLFwIHQWWe5l44aFue+ZvQMGDbypP1DMYw=
Expand Down
167 changes: 167 additions & 0 deletions internal/cmd/git/create/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package create

import (
"context"
"encoding/json"
"fmt"

"github.com/stackitcloud/stackit-cli/internal/cmd/params"

"github.com/goccy/go-yaml"
"github.com/spf13/cobra"
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
"github.com/stackitcloud/stackit-cli/internal/pkg/flags"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-cli/internal/pkg/services/git/client"
"github.com/stackitcloud/stackit-cli/internal/pkg/spinner"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
"github.com/stackitcloud/stackit-sdk-go/services/git"
"github.com/stackitcloud/stackit-sdk-go/services/git/wait"
)

const (
nameFlag = "name"
)

type inputModel struct {
*globalflags.GlobalFlagModel
Id *string
Name string
}

func NewCmd(params *params.CmdParams) *cobra.Command {
cmd := &cobra.Command{
Use: "create",
Short: "Creates STACKIT Git instance",
Long: "Create a STACKIT Git instance by name.",
Args: args.NoArgs,
Example: examples.Build(
examples.NewExample(
`Create a instance with name 'my-new-instance'`,
`$ stackit git create --name my-new-instance`,
),
),
RunE: func(cmd *cobra.Command, _ []string) (err error) {
ctx := context.Background()
model, err := parseInput(params.Printer, cmd)
if err != nil {
return err
}

// Configure API client
apiClient, err := client.ConfigureClient(params.Printer)
if err != nil {
return err
}

if !model.AssumeYes {
prompt := fmt.Sprintf("Are you sure you want to create the instance %q?", model.Name)
err = params.Printer.PromptForConfirmation(prompt)
if err != nil {
return err
}
}

// Call API
request := buildRequest(ctx, model, apiClient)

result, err := request.Execute()
if err != nil {
return fmt.Errorf("create stackit git instance: %w", err)
}
model.Id = result.Id

// Wait for async operation, if async mode not enabled
if !model.Async {
s := spinner.New(params.Printer)
s.Start("Creating stackit git instance")
_, err = wait.CreateGitInstanceWaitHandler(ctx, apiClient, model.ProjectId, *model.Id).WaitWithContext(ctx)
if err != nil {
return fmt.Errorf("wait for stackit git Instance creation: %w", err)
}
s.Stop()
}

return outputResult(params.Printer, model, result)
},
}

configureFlags(cmd)
return cmd
}

func configureFlags(cmd *cobra.Command) {
cmd.Flags().String(nameFlag, "", "The name of the instance.")
if err := flags.MarkFlagsRequired(cmd, nameFlag); err != nil {
cobra.CheckErr(err)
}
}

func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
globalFlags := globalflags.Parse(p, cmd)

if globalFlags.ProjectId == "" {
return nil, &errors.ProjectIdError{}
}
name := flags.FlagToStringValue(p, cmd, nameFlag)

model := inputModel{
GlobalFlagModel: globalFlags,
Name: name,
}

if p.IsVerbosityDebug() {
modelStr, err := print.BuildDebugStrFromInputModel(model)
if err != nil {
p.Debug(print.ErrorLevel, "convert model to string for debugging: %v", err)
} else {
p.Debug(print.DebugLevel, "parsed input values: %s", modelStr)
}
}

return &model, nil
}

func buildRequest(ctx context.Context, model *inputModel, apiClient *git.APIClient) git.ApiCreateInstanceRequest {
return apiClient.CreateInstance(ctx, model.ProjectId).CreateInstancePayload(createPayload(model))
}

func createPayload(model *inputModel) git.CreateInstancePayload {
return git.CreateInstancePayload{
Name: &model.Name,
}
}

func outputResult(p *print.Printer, model *inputModel, resp *git.Instance) error {
if model == nil {
return fmt.Errorf("input model is nil")
}
var outputFormat string
if model.GlobalFlagModel != nil {
outputFormat = model.OutputFormat
}
switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(resp, "", " ")
if err != nil {
return fmt.Errorf("marshal instance: %w", err)
}
p.Outputln(string(details))

return nil
case print.YAMLOutputFormat:
details, err := yaml.MarshalWithOptions(resp, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
if err != nil {
return fmt.Errorf("marshal iminstanceage: %w", err)
}
p.Outputln(string(details))

return nil
default:
p.Outputf("Created instance %q with id %s\n", model.Name, utils.PtrString(model.Id))
return nil
}
}
Loading