Skip to content

Commit

Permalink
(feat) add "src repos {add|update|delete}-metadata -repo-name" flag s…
Browse files Browse the repository at this point in the history
…upport (sourcegraph#977)

* chore(vscode): add cody.codebase setting to use with Cody extension
* update CHANGELOG.md
  • Loading branch information
erzhtor authored Apr 27, 2023
1 parent ef134fb commit 3d540ec
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 27 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
"go.useLanguageServer": true,
"gopls": {
"local": "github.com/sourcegraph/src-cli"
}
},
"cody.codebase": "github.com/sourcegraph/src-cli",
}
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ All notable changes to `src-cli` are documented in this file.
### Added
- `src validate install` can check executor connections [#974](https://github.com/sourcegraph/src-cli/pull/974)
- `src validate install` can send test SMTP message [#973](https://github.com/sourcegraph/src-cli/pull/973)
- `src repos {add|update|delete}-metadata -repo-name` flag support [#977](https://github.com/sourcegraph/src-cli/pull/977)

### Changed
- Renamed `src repo {add|update|delete}-kvp` to `repo {add|update|delete}-metadata` [#972](https://github.com/sourcegraph/src-cli/pull/972)
Expand Down
12 changes: 12 additions & 0 deletions cmd/src/repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"time"

"github.com/sourcegraph/sourcegraph/lib/errors"

"github.com/sourcegraph/src-cli/internal/api"
)

Expand Down Expand Up @@ -125,3 +127,13 @@ func fetchRepositoryID(ctx context.Context, client api.Client, repoName string)
}
return result.Repository.ID, nil
}

func getRepoIdOrError(ctx context.Context, client api.Client, id *string, repoName *string) (*string, error) {
if *id != "" {
return id, nil
} else if *repoName != "" {
repoID, err := fetchRepositoryID(ctx, client, *repoName)
return &repoID, err
}
return nil, errors.New("error: repo or repoName is required")
}
22 changes: 13 additions & 9 deletions cmd/src/repos_add_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"

"github.com/sourcegraph/sourcegraph/lib/errors"

"github.com/sourcegraph/src-cli/internal/api"
)

Expand All @@ -29,19 +30,17 @@ Examples:
fmt.Println(usage)
}
var (
repoFlag = flagSet.String("repo", "", `The ID of the repo to add the key-value pair metadata to (required)`)
keyFlag = flagSet.String("key", "", `The name of the metadata key to add (required)`)
valueFlag = flagSet.String("value", "", `The metadata value associated with the metadata key. Defaults to null.`)
apiFlags = api.NewFlags(flagSet)
repoFlag = flagSet.String("repo", "", `The ID of the repo to add the key-value pair metadata to (required if -repo-name is not specified)`)
repoNameFlag = flagSet.String("repo-name", "", `The name of the repo to add the key-value pair metadata to (required if -repo is not specified)`)
keyFlag = flagSet.String("key", "", `The name of the metadata key to add (required)`)
valueFlag = flagSet.String("value", "", `The metadata value associated with the metadata key. Defaults to null.`)
apiFlags = api.NewFlags(flagSet)
)

handler := func(args []string) error {
if err := flagSet.Parse(args); err != nil {
return err
}
if *repoFlag == "" {
return errors.New("error: repo is required")
}

keyFlag = nil
valueFlag = nil
Expand All @@ -61,6 +60,11 @@ Examples:
}

client := cfg.apiClient(apiFlags, flagSet.Output())
ctx := context.Background()
repoID, err := getRepoIdOrError(ctx, client, repoFlag, repoNameFlag)
if err != nil {
return err
}

query := `mutation addRepoMetadata(
$repo: ID!,
Expand All @@ -77,10 +81,10 @@ Examples:
}`

if ok, err := client.NewRequest(query, map[string]interface{}{
"repo": *repoFlag,
"repo": *repoID,
"key": *keyFlag,
"value": valueFlag,
}).Do(context.Background(), nil); err != nil || !ok {
}).Do(ctx, nil); err != nil || !ok {
return err
}

Expand Down
19 changes: 11 additions & 8 deletions cmd/src/repos_delete_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,16 @@ Examples:
fmt.Println(usage)
}
var (
repoFlag = flagSet.String("repo", "", `The ID of the repo with the key-value pair metadata to be deleted (required)`)
keyFlag = flagSet.String("key", "", `The name of the metadata key to be deleted (required)`)
apiFlags = api.NewFlags(flagSet)
repoFlag = flagSet.String("repo", "", `The ID of the repo with the key-value pair metadata to be deleted (required if -repo-name is not specified)`)
repoNameFlag = flagSet.String("repo-name", "", `The name of the repo to add the key-value pair metadata to (required if -repo is not specified)`)
keyFlag = flagSet.String("key", "", `The name of the metadata key to be deleted (required)`)
apiFlags = api.NewFlags(flagSet)
)

handler := func(args []string) error {
if err := flagSet.Parse(args); err != nil {
return err
}
if *repoFlag == "" {
return errors.New("error: repo is required")
}

keyFlag = nil
flagSet.Visit(func(f *flag.Flag) {
Expand All @@ -53,6 +51,11 @@ Examples:
}

client := cfg.apiClient(apiFlags, flagSet.Output())
ctx := context.Background()
repoID, err := getRepoIdOrError(ctx, client, repoFlag, repoNameFlag)
if err != nil {
return err
}

query := `mutation deleteRepoMetadata(
$repo: ID!,
Expand All @@ -67,9 +70,9 @@ Examples:
}`

if ok, err := client.NewRequest(query, map[string]interface{}{
"repo": *repoFlag,
"repo": *repoID,
"key": *keyFlag,
}).Do(context.Background(), nil); err != nil || !ok {
}).Do(ctx, nil); err != nil || !ok {
return err
}

Expand Down
22 changes: 13 additions & 9 deletions cmd/src/repos_update_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"

"github.com/sourcegraph/sourcegraph/lib/errors"

"github.com/sourcegraph/src-cli/internal/api"
)

Expand All @@ -29,19 +30,17 @@ Examples:
fmt.Println(usage)
}
var (
repoFlag = flagSet.String("repo", "", `The ID of the repo with the metadata key to be updated (required)`)
keyFlag = flagSet.String("key", "", `The name of the metadata key to be updated (required)`)
valueFlag = flagSet.String("value", "", `The new metadata value of the metadata key to be set. Defaults to null.`)
apiFlags = api.NewFlags(flagSet)
repoFlag = flagSet.String("repo", "", `The ID of the repo with the metadata key to be updated (required if -repo-name is not specified)`)
repoNameFlag = flagSet.String("repo-name", "", `The name of the repo to add the key-value pair metadata to (required if -repo is not specified)`)
keyFlag = flagSet.String("key", "", `The name of the metadata key to be updated (required)`)
valueFlag = flagSet.String("value", "", `The new metadata value of the metadata key to be set. Defaults to null.`)
apiFlags = api.NewFlags(flagSet)
)

handler := func(args []string) error {
if err := flagSet.Parse(args); err != nil {
return err
}
if *repoFlag == "" {
return errors.New("error: repo is required")
}

keyFlag = nil
valueFlag = nil
Expand All @@ -61,6 +60,11 @@ Examples:
}

client := cfg.apiClient(apiFlags, flagSet.Output())
ctx := context.Background()
repoID, err := getRepoIdOrError(ctx, client, repoFlag, repoNameFlag)
if err != nil {
return err
}

query := `mutation updateMetadata(
$repo: ID!,
Expand All @@ -77,10 +81,10 @@ Examples:
}`

if ok, err := client.NewRequest(query, map[string]interface{}{
"repo": *repoFlag,
"repo": *repoID,
"key": *keyFlag,
"value": valueFlag,
}).Do(context.Background(), nil); err != nil || !ok {
}).Do(ctx, nil); err != nil || !ok {
return err
}

Expand Down

0 comments on commit 3d540ec

Please sign in to comment.