2023-08-10 09:06:00 +00:00
|
|
|
// Copyright 2023 Woodpecker Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2019-04-06 19:32:14 +00:00
|
|
|
package secret
|
|
|
|
|
2021-10-27 19:03:14 +00:00
|
|
|
import (
|
2023-07-21 17:45:32 +00:00
|
|
|
"fmt"
|
|
|
|
"strconv"
|
2022-08-15 18:10:13 +00:00
|
|
|
"strings"
|
|
|
|
|
2021-10-27 19:03:14 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/cli/internal"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/woodpecker-go/woodpecker"
|
2021-10-27 19:03:14 +00:00
|
|
|
)
|
2019-04-06 19:32:14 +00:00
|
|
|
|
|
|
|
// Command exports the secret command.
|
2021-10-27 19:03:14 +00:00
|
|
|
var Command = &cli.Command{
|
2019-04-06 19:32:14 +00:00
|
|
|
Name: "secret",
|
|
|
|
Usage: "manage secrets",
|
2021-10-27 19:03:14 +00:00
|
|
|
Subcommands: []*cli.Command{
|
2019-04-06 19:32:14 +00:00
|
|
|
secretCreateCmd,
|
|
|
|
secretDeleteCmd,
|
|
|
|
secretUpdateCmd,
|
|
|
|
secretInfoCmd,
|
|
|
|
secretListCmd,
|
|
|
|
},
|
|
|
|
}
|
2022-08-15 18:10:13 +00:00
|
|
|
|
2023-07-21 17:45:32 +00:00
|
|
|
func parseTargetArgs(client woodpecker.Client, c *cli.Context) (global bool, orgID, repoID int64, err error) {
|
2022-08-15 18:10:13 +00:00
|
|
|
if c.Bool("global") {
|
2023-07-21 17:45:32 +00:00
|
|
|
return true, -1, -1, nil
|
2022-08-15 18:10:13 +00:00
|
|
|
}
|
2023-06-12 23:07:52 +00:00
|
|
|
|
|
|
|
repoIDOrFullName := c.String("repository")
|
|
|
|
if repoIDOrFullName == "" {
|
|
|
|
repoIDOrFullName = c.Args().First()
|
2022-08-15 18:10:13 +00:00
|
|
|
}
|
2023-06-12 23:07:52 +00:00
|
|
|
|
2023-07-21 17:45:32 +00:00
|
|
|
orgIDOrName := c.String("organization")
|
|
|
|
if orgIDOrName == "" && repoIDOrFullName == "" {
|
|
|
|
if err := cli.ShowSubcommandHelp(c); err != nil {
|
|
|
|
return false, -1, -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, -1, -1, fmt.Errorf("missing arguments")
|
|
|
|
}
|
|
|
|
|
|
|
|
if orgIDOrName != "" && repoIDOrFullName == "" {
|
|
|
|
if orgID, err := strconv.ParseInt(orgIDOrName, 10, 64); err == nil {
|
|
|
|
return false, orgID, -1, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
org, err := client.OrgLookup(orgIDOrName)
|
|
|
|
if err != nil {
|
|
|
|
return false, -1, -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, org.ID, -1, nil
|
2022-08-15 18:10:13 +00:00
|
|
|
}
|
2023-06-12 23:07:52 +00:00
|
|
|
|
2023-07-21 17:45:32 +00:00
|
|
|
if orgIDOrName != "" && !strings.Contains(repoIDOrFullName, "/") {
|
|
|
|
repoIDOrFullName = orgIDOrName + "/" + repoIDOrFullName
|
2022-08-15 18:10:13 +00:00
|
|
|
}
|
2023-06-12 23:07:52 +00:00
|
|
|
|
|
|
|
repoID, err = internal.ParseRepo(client, repoIDOrFullName)
|
2022-08-15 18:10:13 +00:00
|
|
|
if err != nil {
|
2023-07-21 17:45:32 +00:00
|
|
|
return false, -1, -1, err
|
2022-08-15 18:10:13 +00:00
|
|
|
}
|
2023-06-12 23:07:52 +00:00
|
|
|
|
2023-07-21 17:45:32 +00:00
|
|
|
return false, -1, repoID, nil
|
2022-08-15 18:10:13 +00:00
|
|
|
}
|