2023-08-10 11:06:00 +02: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 21:32:14 +02:00
|
|
|
package secret
|
|
|
|
|
2021-10-27 21:03:14 +02:00
|
|
|
import (
|
2023-07-21 19:45:32 +02:00
|
|
|
"fmt"
|
|
|
|
"strconv"
|
2022-08-15 20:10:13 +02:00
|
|
|
"strings"
|
|
|
|
|
2021-10-27 21:03:14 +02:00
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
|
|
|
|
"github.com/woodpecker-ci/woodpecker/cli/common"
|
2022-08-15 20:10:13 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/cli/internal"
|
2023-06-13 01:07:52 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/woodpecker-go/woodpecker"
|
2021-10-27 21:03:14 +02:00
|
|
|
)
|
2019-04-06 21:32:14 +02:00
|
|
|
|
|
|
|
// Command exports the secret command.
|
2021-10-27 21:03:14 +02:00
|
|
|
var Command = &cli.Command{
|
2019-04-06 21:32:14 +02:00
|
|
|
Name: "secret",
|
|
|
|
Usage: "manage secrets",
|
2021-10-27 21:03:14 +02:00
|
|
|
Flags: common.GlobalFlags,
|
|
|
|
Subcommands: []*cli.Command{
|
2019-04-06 21:32:14 +02:00
|
|
|
secretCreateCmd,
|
|
|
|
secretDeleteCmd,
|
|
|
|
secretUpdateCmd,
|
|
|
|
secretInfoCmd,
|
|
|
|
secretListCmd,
|
|
|
|
},
|
|
|
|
}
|
2022-08-15 20:10:13 +02:00
|
|
|
|
2023-07-21 19:45:32 +02:00
|
|
|
func parseTargetArgs(client woodpecker.Client, c *cli.Context) (global bool, orgID, repoID int64, err error) {
|
2022-08-15 20:10:13 +02:00
|
|
|
if c.Bool("global") {
|
2023-07-21 19:45:32 +02:00
|
|
|
return true, -1, -1, nil
|
2022-08-15 20:10:13 +02:00
|
|
|
}
|
2023-06-13 01:07:52 +02:00
|
|
|
|
|
|
|
repoIDOrFullName := c.String("repository")
|
|
|
|
if repoIDOrFullName == "" {
|
|
|
|
repoIDOrFullName = c.Args().First()
|
2022-08-15 20:10:13 +02:00
|
|
|
}
|
2023-06-13 01:07:52 +02:00
|
|
|
|
2023-07-21 19:45:32 +02: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 20:10:13 +02:00
|
|
|
}
|
2023-06-13 01:07:52 +02:00
|
|
|
|
2023-07-21 19:45:32 +02:00
|
|
|
if orgIDOrName != "" && !strings.Contains(repoIDOrFullName, "/") {
|
|
|
|
repoIDOrFullName = orgIDOrName + "/" + repoIDOrFullName
|
2022-08-15 20:10:13 +02:00
|
|
|
}
|
2023-06-13 01:07:52 +02:00
|
|
|
|
|
|
|
repoID, err = internal.ParseRepo(client, repoIDOrFullName)
|
2022-08-15 20:10:13 +02:00
|
|
|
if err != nil {
|
2023-07-21 19:45:32 +02:00
|
|
|
return false, -1, -1, err
|
2022-08-15 20:10:13 +02:00
|
|
|
}
|
2023-06-13 01:07:52 +02:00
|
|
|
|
2023-07-21 19:45:32 +02:00
|
|
|
return false, -1, repoID, nil
|
2022-08-15 20:10:13 +02:00
|
|
|
}
|