Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Add custom prefix devurl #74

Merged
merged 7 commits into from
Aug 4, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add urls ls subcmd; add -o [human | json] support matching users cmd
  • Loading branch information
Russtopia committed Jul 30, 2020
commit c71c3c9f5c645e97241d8a6ff953ea2db569f2b8
90 changes: 62 additions & 28 deletions cmd/coder/urls.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,26 @@ import (

type urlsCmd struct{}

func (cmd *urlsCmd) Subcommands() []cli.Command {
return []cli.Command{
&listSubCmd{},
&createSubCmd{},
&delSubCmd{},
}
}

func (cmd urlsCmd) Spec() cli.CommandSpec {
return cli.CommandSpec{
Name: "urls",
Usage: "[subcommand] <flags>",
Desc: "interact with environment devurls",
}
}

func (cmd urlsCmd) Run(fl *pflag.FlagSet) {
exitUsage(fl)
}

// DevURL is the parsed json response record for a devURL from cemanager
type DevURL struct {
ID string `json:"id"`
Expand Down Expand Up @@ -57,6 +77,48 @@ func accessLevelIsValid(level string) bool {
return ok
}

type listSubCmd struct {
outputFmt string
}

// Run gets the list of active devURLs from the cemanager for the
// specified environment and outputs info to stdout.
func (sub listSubCmd) Run(fl *pflag.FlagSet) {
envName := fl.Arg(0)
devURLs := urlList(envName)

switch sub.outputFmt {
case "human":
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.TabIndent)
for _, devURL := range devURLs {
fmt.Fprintf(w, "%s\t%d\t%s\n", devURL.URL, devURL.Port, devURL.Access)
}
err := w.Flush()
if err != nil {
flog.Fatal("failed to flush writer: %v", err)
}
case "json":
err := json.NewEncoder(os.Stdout).Encode(devURLs)
if err != nil {
flog.Fatal("failed to encode devurls to json: %v", err)
}
default:
exitUsage(fl)
}
}

func (sub *listSubCmd) RegisterFlags(fl *pflag.FlagSet) {
fl.StringVarP(&sub.outputFmt, "output", "o", "human", "output format (human | json)")
}

func (sub *listSubCmd) Spec() cli.CommandSpec {
return cli.CommandSpec{
Name: "ls",
Usage: "<env> <flags>",
Desc: "list all devurls",
}
}

type createSubCmd struct {
access string
urlname string
Expand Down Expand Up @@ -181,14 +243,6 @@ func (sub delSubCmd) Run(fl *pflag.FlagSet) {
}
}

func (cmd urlsCmd) Spec() cli.CommandSpec {
return cli.CommandSpec{
Name: "urls",
Usage: "<env name>",
Desc: "get all development urls for external access",
}
}

// urlList returns the list of active devURLs from the cemanager.
func urlList(envName string) []DevURL {
entClient := requireAuth()
Expand Down Expand Up @@ -217,23 +271,3 @@ func urlList(envName string) []DevURL {

return devURLs
}

// Run gets the list of active devURLs from the cemanager for the
// specified environment and outputs info to stdout.
func (cmd urlsCmd) Run(fl *pflag.FlagSet) {
envName := fl.Arg(0)
devURLs := urlList(envName)

w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.TabIndent)
for _, devURL := range devURLs {
fmt.Fprintf(w, "%s\t%d\t%s\n", devURL.URL, devURL.Port, devURL.Access)
}
w.Flush()
}

func (cmd *urlsCmd) Subcommands() []cli.Command {
return []cli.Command{
&createSubCmd{},
&delSubCmd{},
}
}