Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 an STACKIT Git instance by name.

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

### Examples

```
Create an 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 an STACKIT Git instance by its internal ID.

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

### Examples

```
Delete an 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 an 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

40 changes: 40 additions & 0 deletions docs/stackit_git_list.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
## 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
```

### Options

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

### 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 @@ -77,6 +77,7 @@ require (
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.12.0 // indirect
github.com/spf13/cast v1.7.1 // indirect
github.com/stackitcloud/stackit-sdk-go/services/git v0.3.1
github.com/stackitcloud/stackit-sdk-go/services/loadbalancer v1.0.2
github.com/stackitcloud/stackit-sdk-go/services/logme v0.22.1
github.com/stackitcloud/stackit-sdk-go/services/mariadb v0.22.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ github.com/stackitcloud/stackit-sdk-go/services/authorization v0.6.2 h1:erpN0BM7
github.com/stackitcloud/stackit-sdk-go/services/authorization v0.6.2/go.mod h1:dJ19ZwFjp2bfC5ZobXV3vUdSpE3quUw3GuoFSKLpHIo=
github.com/stackitcloud/stackit-sdk-go/services/dns v0.13.2 h1:6rb3EM0yXuMIBd1U6WsJoMzEiVaHC3WQFWFvT23OE4Y=
github.com/stackitcloud/stackit-sdk-go/services/dns v0.13.2/go.mod h1:PMHoavoIaRZpkI9BA0nsnRjGoHASVSBon45XB3QyhMA=
github.com/stackitcloud/stackit-sdk-go/services/git v0.3.1 h1:1QYUya5bstfd/XdHtQNQU2kHDp1Ikz/e2Zc9o3VSG48=
github.com/stackitcloud/stackit-sdk-go/services/git v0.3.1/go.mod h1:XhXHJpOVC9Rpwyf1G+EpMbprBafH9aZb8vWBdR+z0WM=
github.com/stackitcloud/stackit-sdk-go/services/iaas v0.22.1 h1:JXcLcbVesTtwVVb+jJjU3o0FmSpXBRnOw6PVETaeK+E=
github.com/stackitcloud/stackit-sdk-go/services/iaas v0.22.1/go.mod h1:QNH50Pq0Hu21lLDOwa02PIjRjTl0LfEdHoz5snGQRn8=
github.com/stackitcloud/stackit-sdk-go/services/loadbalancer v1.0.2 h1:5rVt3n7kDJvJQxFCtxfx8uZI9PGkvJY9fVJ4yar10Uc=
Expand Down
165 changes: 165 additions & 0 deletions internal/cmd/git/create/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package create

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

"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(p *print.Printer) *cobra.Command {
cmd := &cobra.Command{
Use: "create",
Short: "Creates STACKIT Git instance",
Long: "Create an STACKIT Git instance by name.",
Args: args.NoArgs,
Example: examples.Build(
examples.NewExample(
`Create an 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(p, cmd)
if err != nil {
return err
}

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

if !model.AssumeYes {
prompt := fmt.Sprintf("Are you sure you want to create the instance %q?", model.Name)
err = p.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(p)
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(p, 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