mirror of
https://github.com/drone/drone-cli.git
synced 2024-11-23 01:11:57 +01:00
Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
a5f8e7c245
@ -2,10 +2,12 @@ package jsonnet
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/drone/drone-yaml/yaml"
|
||||
"github.com/drone/drone-yaml/yaml/pretty"
|
||||
@ -51,6 +53,10 @@ var Command = cli.Command{
|
||||
Name: "string",
|
||||
Usage: "Expect a string, manifest as plain text",
|
||||
},
|
||||
cli.StringSliceFlag{
|
||||
Name: "extVar, V",
|
||||
Usage: "Pass extVars to Jsonnet (can be specified multiple times)",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
@ -74,6 +80,16 @@ func generate(c *cli.Context) error {
|
||||
// register native functions
|
||||
RegisterNativeFuncs(vm)
|
||||
|
||||
// extVars
|
||||
vars := c.StringSlice("extVar")
|
||||
for _, v := range vars {
|
||||
name, value, err := getVarVal(v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
vm.ExtVar(name, value)
|
||||
}
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
if c.Bool("stream") {
|
||||
docs, err := vm.EvaluateSnippetStream(source, string(data))
|
||||
@ -114,3 +130,17 @@ func generate(c *cli.Context) error {
|
||||
|
||||
return ioutil.WriteFile(target, buf.Bytes(), 0644)
|
||||
}
|
||||
|
||||
// https://github.com/google/go-jsonnet/blob/master/cmd/jsonnet/cmd.go#L149
|
||||
func getVarVal(s string) (string, string, error) {
|
||||
parts := strings.SplitN(s, "=", 2)
|
||||
name := parts[0]
|
||||
if len(parts) == 1 {
|
||||
content, exists := os.LookupEnv(name)
|
||||
if exists {
|
||||
return name, content, nil
|
||||
}
|
||||
return "", "", fmt.Errorf("environment variable %v was undefined", name)
|
||||
}
|
||||
return name, parts[1], nil
|
||||
}
|
||||
|
@ -20,16 +20,20 @@ var serverListCmd = cli.Command{
|
||||
ArgsUsage: " ",
|
||||
Action: serverList,
|
||||
Flags: []cli.Flag{
|
||||
cli.StringFlag{
|
||||
Name: "s, state",
|
||||
Usage: "filter by state",
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "a",
|
||||
Name: "a, all",
|
||||
Usage: "include stopped servers",
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "l",
|
||||
Name: "l, long",
|
||||
Usage: "list in long format",
|
||||
},
|
||||
cli.BoolTFlag{
|
||||
Name: "H",
|
||||
Name: "H, headers",
|
||||
Usage: "include column headers",
|
||||
},
|
||||
cli.StringFlag{
|
||||
@ -49,6 +53,7 @@ func serverList(c *cli.Context) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s := c.String("s")
|
||||
a := c.Bool("a")
|
||||
l := c.Bool("l")
|
||||
h := c.BoolT("H")
|
||||
@ -64,7 +69,7 @@ func serverList(c *cli.Context) error {
|
||||
}
|
||||
|
||||
if l && h {
|
||||
printLong(servers, a, h)
|
||||
printLong(servers, s, a, h)
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -77,12 +82,17 @@ func serverList(c *cli.Context) error {
|
||||
if !a && server.State == "stopped" {
|
||||
continue
|
||||
}
|
||||
|
||||
if s != "" && s != server.State {
|
||||
continue
|
||||
}
|
||||
|
||||
tmpl.Execute(os.Stdout, server)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func printLong(servers []*drone.Server, a, h bool) {
|
||||
func printLong(servers []*drone.Server, s string, a, h bool) {
|
||||
w := tabwriter.NewWriter(os.Stdout, 0, 0, 4, ' ', 0)
|
||||
if h {
|
||||
fmt.Fprintln(w, "Name\tAddress\tState\tCreated")
|
||||
@ -91,6 +101,9 @@ func printLong(servers []*drone.Server, a, h bool) {
|
||||
if !a && server.State == "stopped" {
|
||||
continue
|
||||
}
|
||||
if s != "" && s != server.State {
|
||||
continue
|
||||
}
|
||||
fmt.Fprintf(w, "%s\t%s\t%s\t%s ago\n",
|
||||
server.Name,
|
||||
server.Address,
|
||||
|
Loading…
Reference in New Issue
Block a user