Skip to content

Commit

Permalink
Upgrade urfave/cli to v2 (#483)
Browse files Browse the repository at this point in the history
* migrate urfave/ci v1 -> v2
* refactor cli (format flag)
* log error if agent can not listen on port 3000

close #452
  • Loading branch information
6543 committed Oct 27, 2021
1 parent d35f515 commit 798c2bc
Show file tree
Hide file tree
Showing 121 changed files with 8,529 additions and 1,468 deletions.
11 changes: 8 additions & 3 deletions cli/build/build.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package build

import "github.com/urfave/cli"
import (
"github.com/urfave/cli/v2"

"github.com/woodpecker-ci/woodpecker/cli/common"
)

// Command exports the build command set.
var Command = cli.Command{
var Command = &cli.Command{
Name: "build",
Usage: "manage builds",
Subcommands: []cli.Command{
Flags: common.GlobalFlags,
Subcommands: []*cli.Command{
buildListCmd,
buildLastCmd,
buildLogsCmd,
Expand Down
6 changes: 4 additions & 2 deletions cli/build/build_approve.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ import (
"fmt"
"strconv"

"github.com/urfave/cli"
"github.com/urfave/cli/v2"

"github.com/woodpecker-ci/woodpecker/cli/common"
"github.com/woodpecker-ci/woodpecker/cli/internal"
)

var buildApproveCmd = cli.Command{
var buildApproveCmd = &cli.Command{
Name: "approve",
Usage: "approve a build",
ArgsUsage: "<repo/name> <build>",
Action: buildApprove,
Flags: common.GlobalFlags,
}

func buildApprove(c *cli.Context) (err error) {
Expand Down
6 changes: 4 additions & 2 deletions cli/build/build_decline.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ import (
"fmt"
"strconv"

"github.com/urfave/cli"
"github.com/urfave/cli/v2"

"github.com/woodpecker-ci/woodpecker/cli/common"
"github.com/woodpecker-ci/woodpecker/cli/internal"
)

var buildDeclineCmd = cli.Command{
var buildDeclineCmd = &cli.Command{
Name: "decline",
Usage: "decline a build",
ArgsUsage: "<repo/name> <build>",
Action: buildDecline,
Flags: common.GlobalFlags,
}

func buildDecline(c *cli.Context) (err error) {
Expand Down
15 changes: 6 additions & 9 deletions cli/build/build_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,20 @@ import (
"strconv"
"text/template"

"github.com/urfave/cli"
"github.com/urfave/cli/v2"

"github.com/woodpecker-ci/woodpecker/cli/common"
"github.com/woodpecker-ci/woodpecker/cli/internal"
)

var buildInfoCmd = cli.Command{
var buildInfoCmd = &cli.Command{
Name: "info",
Usage: "show build details",
ArgsUsage: "<repo/name> [build]",
Action: buildInfo,
Flags: []cli.Flag{
cli.StringFlag{
Name: "format",
Usage: "format output",
Value: tmplBuildInfo,
},
},
Flags: append(common.GlobalFlags,
common.FormatFlag(tmplBuildInfo),
),
}

func buildInfo(c *cli.Context) error {
Expand Down
6 changes: 4 additions & 2 deletions cli/build/build_kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ import (
"fmt"
"strconv"

"github.com/urfave/cli"
"github.com/urfave/cli/v2"

"github.com/woodpecker-ci/woodpecker/cli/common"
"github.com/woodpecker-ci/woodpecker/cli/internal"
)

var buildKillCmd = cli.Command{
var buildKillCmd = &cli.Command{
Name: "kill",
Usage: "force kill a build",
ArgsUsage: "<repo/name> <build>",
Action: buildKill,
Hidden: true,
Flags: common.GlobalFlags,
}

func buildKill(c *cli.Context) (err error) {
Expand Down
17 changes: 7 additions & 10 deletions cli/build/build_last.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,25 @@ import (
"os"
"text/template"

"github.com/urfave/cli"
"github.com/urfave/cli/v2"

"github.com/woodpecker-ci/woodpecker/cli/common"
"github.com/woodpecker-ci/woodpecker/cli/internal"
)

var buildLastCmd = cli.Command{
var buildLastCmd = &cli.Command{
Name: "last",
Usage: "show latest build details",
ArgsUsage: "<repo/name>",
Action: buildLast,
Flags: []cli.Flag{
cli.StringFlag{
Name: "format",
Usage: "format output",
Value: tmplBuildInfo,
},
cli.StringFlag{
Flags: append(common.GlobalFlags,
common.FormatFlag(tmplBuildInfo),
&cli.StringFlag{
Name: "branch",
Usage: "branch name",
Value: "master",
},
},
),
}

func buildLast(c *cli.Context) error {
Expand Down
23 changes: 10 additions & 13 deletions cli/build/build_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,37 @@ import (
"os"
"text/template"

"github.com/urfave/cli"
"github.com/urfave/cli/v2"

"github.com/woodpecker-ci/woodpecker/cli/common"
"github.com/woodpecker-ci/woodpecker/cli/internal"
)

var buildListCmd = cli.Command{
var buildListCmd = &cli.Command{
Name: "ls",
Usage: "show build history",
ArgsUsage: "<repo/name>",
Action: buildList,
Flags: []cli.Flag{
cli.StringFlag{
Name: "format",
Usage: "format output",
Value: tmplBuildList,
},
cli.StringFlag{
Flags: append(common.GlobalFlags,
common.FormatFlag(tmplBuildList),
&cli.StringFlag{
Name: "branch",
Usage: "branch filter",
},
cli.StringFlag{
&cli.StringFlag{
Name: "event",
Usage: "event filter",
},
cli.StringFlag{
&cli.StringFlag{
Name: "status",
Usage: "status filter",
},
cli.IntFlag{
&cli.IntFlag{
Name: "limit",
Usage: "limit the list size",
Value: 25,
},
},
),
}

func buildList(c *cli.Context) error {
Expand Down
7 changes: 5 additions & 2 deletions cli/build/build_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ package build
import (
"fmt"

"github.com/urfave/cli"
"github.com/woodpecker-ci/woodpecker/cli/common"

"github.com/urfave/cli/v2"
)

var buildLogsCmd = cli.Command{
var buildLogsCmd = &cli.Command{
Name: "logs",
Usage: "show build logs",
ArgsUsage: "<repo/name> [build] [job]",
Action: buildLogs,
Flags: common.GlobalFlags,
}

func buildLogs(c *cli.Context) error {
Expand Down
15 changes: 6 additions & 9 deletions cli/build/build_ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,20 @@ import (
"strconv"
"text/template"

"github.com/urfave/cli"
"github.com/urfave/cli/v2"

"github.com/woodpecker-ci/woodpecker/cli/common"
"github.com/woodpecker-ci/woodpecker/cli/internal"
)

var buildPsCmd = cli.Command{
var buildPsCmd = &cli.Command{
Name: "ps",
Usage: "show build steps",
ArgsUsage: "<repo/name> [build]",
Action: buildPs,
Flags: []cli.Flag{
cli.StringFlag{
Name: "format",
Usage: "format output",
Value: tmplBuildPs,
},
},
Flags: append(common.GlobalFlags,
common.FormatFlag(tmplBuildPs),
),
}

func buildPs(c *cli.Context) error {
Expand Down
15 changes: 6 additions & 9 deletions cli/build/build_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,20 @@ import (
"os"
"text/template"

"github.com/urfave/cli"
"github.com/urfave/cli/v2"

"github.com/woodpecker-ci/woodpecker/cli/common"
"github.com/woodpecker-ci/woodpecker/cli/internal"
)

var buildQueueCmd = cli.Command{
var buildQueueCmd = &cli.Command{
Name: "queue",
Usage: "show build queue",
ArgsUsage: " ",
Action: buildQueue,
Flags: []cli.Flag{
cli.StringFlag{
Name: "format",
Usage: "format output",
Value: tmplBuildQueue,
},
},
Flags: append(common.GlobalFlags,
common.FormatFlag(tmplBuildQueue),
),
}

func buildQueue(c *cli.Context) error {
Expand Down
16 changes: 9 additions & 7 deletions cli/build/build_start.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,24 @@ import (
"fmt"
"strconv"

"github.com/urfave/cli"
"github.com/urfave/cli/v2"

"github.com/woodpecker-ci/woodpecker/cli/common"
"github.com/woodpecker-ci/woodpecker/cli/internal"
)

var buildStartCmd = cli.Command{
var buildStartCmd = &cli.Command{
Name: "start",
Usage: "start a build",
ArgsUsage: "<repo/name> [build]",
Action: buildStart,
Flags: []cli.Flag{
cli.StringSliceFlag{
Name: "param, p",
Usage: "custom parameters to be injected into the job environment. Format: KEY=value",
Flags: append(common.GlobalFlags,
&cli.StringSliceFlag{
Name: "param",
Aliases: []string{"p"},
Usage: "custom parameters to be injected into the job environment. Format: KEY=value",
},
},
),
}

func buildStart(c *cli.Context) (err error) {
Expand Down
6 changes: 4 additions & 2 deletions cli/build/build_stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ import (
"fmt"
"strconv"

"github.com/urfave/cli"
"github.com/urfave/cli/v2"

"github.com/woodpecker-ci/woodpecker/cli/common"
"github.com/woodpecker-ci/woodpecker/cli/internal"
)

var buildStopCmd = cli.Command{
var buildStopCmd = &cli.Command{
Name: "stop",
Usage: "stop a build",
ArgsUsage: "<repo/name> [build] [job]",
Flags: common.GlobalFlags,
Action: buildStop,
}

Expand Down
61 changes: 61 additions & 0 deletions cli/common/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2021 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.

package common

import "github.com/urfave/cli/v2"

var GlobalFlags = []cli.Flag{
&cli.StringFlag{
EnvVars: []string{"WOODPECKER_TOKEN"},
Name: "token",
Aliases: []string{"t"},
Usage: "server auth token",
},
&cli.StringFlag{
EnvVars: []string{"WOODPECKER_SERVER"},
Name: "server",
Aliases: []string{"s"},
Usage: "server address",
},
&cli.BoolFlag{
EnvVars: []string{"WOODPECKER_SKIP_VERIFY"},
Name: "skip-verify",
Usage: "skip ssl verification",
Hidden: true,
},
&cli.StringFlag{
EnvVars: []string{"SOCKS_PROXY"},
Name: "socks-proxy",
Usage: "socks proxy address",
Hidden: true,
},
&cli.BoolFlag{
EnvVars: []string{"SOCKS_PROXY_OFF"},
Name: "socks-proxy-off",
Usage: "socks proxy ignored",
Hidden: true,
},
}

// FormatFlag return format flag with value set based on template
// if hidden value is set, flag will be hidden
func FormatFlag(tmpl string, hidden ...bool) *cli.StringFlag {
return &cli.StringFlag{
Name: "format",
Usage: "format output",
Value: tmpl,
Hidden: len(hidden) != 0,
}
}
Loading

0 comments on commit 798c2bc

Please sign in to comment.