Skip to content
This repository has been archived by the owner on Jul 24, 2021. It is now read-only.

Commit

Permalink
Merge pull request #79 from joyent/chris/fix-KOSH_URL-env
Browse files Browse the repository at this point in the history
Fix KOSH_URL environment variable
  • Loading branch information
perigrin authored Nov 17, 2020
2 parents 767b579 + 366337e commit 4144095
Show file tree
Hide file tree
Showing 9 changed files with 283 additions and 146 deletions.
28 changes: 10 additions & 18 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
const (
productionURL = "https://conch.joyent.us"
stagingURL = "https://staging.conch.joyent.us"
edgeURL = "https://edge.conch.joyent.us"
)

func fatal(e error) {
Expand Down Expand Up @@ -44,11 +43,7 @@ func requireSysAdmin(c Config) {
var config Config

// NewApp creates a new kosh app, takes a cli.Config and returns an instance of cli.Cli
func NewApp(cfg Config) *cli.Cli {
var URLSetByUser bool

config = cfg

func NewApp(config Config) *cli.Cli {
app := cli.App("kosh", "Command line interface for Conch")
app.Spec = "[-dejutvV]"

Expand All @@ -58,22 +53,21 @@ func NewApp(cfg Config) *cli.Cli {
Name: "t token",
Value: "",
Desc: "API token",
EnvVar: "KOSH_TOKEN",
EnvVar: "KOSH_TOKEN CONCH_TOKEN",
})

app.StringPtr(&config.ConchENV, cli.StringOpt{
Name: "env e",
Value: "production",
Desc: "This specifies the environment KOSH is pointing to",
EnvVar: "KOSH_ENV",
EnvVar: "KOSH_ENV CONCH_ENV",
})

app.StringPtr(&config.ConchURL, cli.StringOpt{
Name: "u url",
Value: productionURL,
Desc: "This specifies the API URL.",
EnvVar: "KOSH_URL",
SetByUser: &URLSetByUser,
Name: "u url",
Value: "",
Desc: "This specifies the API URL.",
EnvVar: "KOSH_URL CONCH_URL",
})

app.BoolPtr(&config.OutputJSON, cli.BoolOpt{
Expand All @@ -87,14 +81,14 @@ func NewApp(cfg Config) *cli.Cli {
Name: "d debug",
Value: false,
Desc: "Enable Debugging output (for debugging purposes *very* noisy). ",
EnvVar: "KOSH_DEBUG_MODE",
EnvVar: "KOSH_DEBUG_MODE KOSH_DEBUG", // TODO in 4.0 remove KOSH_DEBUG_MODE
})

app.BoolPtr(&config.Logger.LevelInfo, cli.BoolOpt{
Name: "v verbose",
Value: false,
Desc: "Enable Verbose Output",
EnvVar: "KOSH_VERBOSE_MODE",
EnvVar: "KOSH_VERBOSE_MODE KOSH_VERBOSE", // TODO in 4.0 remove KOSH_VERBOSE_MODE
})

app.Command("build b", "Work with a specific build", buildCmd)
Expand Down Expand Up @@ -134,14 +128,12 @@ func NewApp(cfg Config) *cli.Cli {
})

app.Before = func() {
if !URLSetByUser {
if config.ConchURL == "" {
switch config.ConchENV {
case "production":
config.ConchURL = productionURL
case "staging":
config.ConchURL = stagingURL
case "edge":
config.ConchURL = edgeURL
default:
fatal(errors.New("environment not one of production, staging, edge: perhaps you want --url?"))
}
Expand Down
4 changes: 4 additions & 0 deletions cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,17 @@ func (c Config) ConchClient() *conch.Client {
)
}

// Renderer is a function that takes some kind of data and an error and renders
// the output
type Renderer func(interface{}, error)

// Renderer returns a function that will render to STDOUT
func (c Config) Renderer() Renderer {
return c.RenderTo(os.Stdout)
}

// Before takes a list of checks and initializers and returns a function
// suitable for running in a commmand's before block
func (c Config) Before(checks ...func(c Config)) func() {
return func() {
for _, check := range checks {
Expand Down
27 changes: 14 additions & 13 deletions conch/builds.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ func (c *Client) GetBuildByID(id types.UUID) (build types.Build, e error) {
return
}

// UpdateBuild updates a named build
// POST /build/:build_id_or_name
// UpdateBuild (POST /build/:build_id_or_name) updates a named build
func (c *Client) UpdateBuild(name string, update types.BuildUpdate) error {
return c.UpdateBuildByName(name, update)
}
Expand All @@ -58,53 +57,55 @@ func (c *Client) UpdateBuildByID(buildID types.UUID, update types.BuildUpdate) e
return e
}

// UpdateBuild updates a named build
// POST /build/:build_id_or_name
// UpdateBuildByName (POST /build/:build_id_or_name) updates a named build
func (c *Client) UpdateBuildByName(name string, update types.BuildUpdate) error {
c.Logger.Info(fmt.Sprintf("updating build %v: %v", name, update))
_, e := c.Build(name).Post(update).Send()
return e
}

// GetBuildUsers retrieves a list of users associated with the given build
// GET /build/:build_id_or_name/user
// GetBuildUsers (GET /build/:build_id_or_name/user) retrieves a list of users
// associated with the given build
func (c *Client) GetBuildUsers(name string) (build types.BuildUsers, e error) {
c.Logger.Info(fmt.Sprintf("getting users for build: %s", name))
_, e = c.Build(name).User("").Receive(&build)
return
}

// AddBuildUser associates a new user with the build, optionally tell the API to email the user too
// POST /build/:build_id_or_name/user
// AddBuildUser (POST /build/:build_id_or_name/user) associates a new user with
// the build, optionally tell the API to email the user too
func (c *Client) AddBuildUser(name string, update types.BuildAddUser, sendEmail bool) error {
c.Logger.Info(fmt.Sprintf("adding users to build %v: %v", name, update))
_, e := c.Build(name).User("").Post(update).Send()
return e
}

// DeleteBuildUser removes a user from being associated with the build
// DELETE /build/:build_id_or_name/user/#target_user_id_or_email
// DeleteBuildUser (DELETE /build/:build_id_or_name/user/#target_user_id_or_email)
// removes a user from being associated with the build
func (c *Client) DeleteBuildUser(name, user string, sendEmail bool) error {
c.Logger.Info(fmt.Sprintf("removing user from build %v: %v", name, user))
_, e := c.Build(name).User(user).Delete().Send()
return e
}

// GetAllBuildOrganizations - GET /build/:build_id_or_name/user
// GetAllBuildOrganizations (GET /build/:build_id_or_name/user) retrieves a
// list of all organizations associated with the named build
func (c *Client) GetAllBuildOrganizations(name string) (build types.BuildOrganizations, e error) {
c.Logger.Info(fmt.Sprintf("getting organizations for build: %s", name))
_, e = c.Build(name).Organization("").Receive(&build)
return
}

// AddBuildOrganization - POST /build/:build_id_or_name/user
// AddBuildOrganization (POST /build/:build_id_or_name/user) adds an
// organization to the named build.
func (c *Client) AddBuildOrganization(name string, update types.BuildAddOrganization, sendEmail bool) error {
c.Logger.Info(fmt.Sprintf("adding organization to build %v: %v", name, update))
_, e := c.Build(name).Organization("").Post(update).Send()
return e
}

// DeleteBuildOrganization - DELETE /build/:build_id_or_name/user/#target_user_id_or_email
// DeleteBuildOrganization (DELETE /build/:build_id_or_name/user/#target_user_id_or_email)
// removes an organization from the named build
func (c *Client) DeleteBuildOrganization(build, org string, sendEmail bool) error {
c.Logger.Info(fmt.Sprintf("removing organization from build %v: %v", build, org))
_, e := c.Build(build).Organization(org).Delete().Send()
Expand Down
16 changes: 10 additions & 6 deletions conch/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ func defaultUserAgent() string {
return fmt.Sprintf("go-conch %s", filepath.Base(f))
}

// Option is a function that takes a Conch client pointer and applies a
// configuration option to it
type Option func(*Client)

// New takes a Config struct and returns a new instance of Client
Expand All @@ -45,27 +47,28 @@ func New(options ...Option) (client *Client) {
return
}

// HTTPCLient sets the client used by the package for making HTTP Requests
// HTTPClient returns an Option that sets the client used by the package for
// making HTTP Requests
func HTTPClient(client *http.Client) Option {
return func(c *Client) { c.Sling.Client(client) }
}

// UserAgent sets the User-Agent used by the package
// UserAgent returns an Option that sets the User-Agent used by the package
func UserAgent(ua string) Option {
return func(c *Client) { c.Sling.Set("User-Agent", ua) }
}

// API sets the base URL for the API
// API returns an Option that sets the base URL for the API
func API(url string) Option {
return func(c *Client) { c.Sling.Base(url) }
}

// AuthToken sets the authentication token
// AuthToken returns an Option that sets the authentication token
func AuthToken(token string) Option {
return func(c *Client) { c.Sling.Set("Authorization", "Bearer "+token) }
}

// Logger sets the logger used by the package
// Logger returns an Option that sets the logger used by the package
func Logger(logger logger.Interface) Option {
return func(c *Client) { c.Logger = logger }
}
Expand All @@ -84,13 +87,14 @@ func (c *Client) New() *Client {
return &Client{s, l}
}

// UserAgent sets the client's User-Agent header to the given string
// UserAgent sets the client's User-Agent header in the request
func (c *Client) UserAgent(ua string) *Client {
c = c.New()
c.Sling.Set("User-Agent", ua)
return c
}

// Authorization sets the Authorization header in the request
func (c *Client) Authorization(auth string) *Client {
c = c.New()
c.Sling.Set("Authorization", auth)
Expand Down
36 changes: 36 additions & 0 deletions conch/types/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,64 @@ import (
"github.com/gofrs/uuid"
)

// UUID is a Universally Unique ID
type UUID struct{ uuid.UUID }

// MojoRelaxedPlaceholder is a string
type MojoRelaxedPlaceholder string

// MojoStandardPlaceholder is a string
type MojoStandardPlaceholder string

// Link is a string
type Link string

// DeviceSerialNumber is a string
type DeviceSerialNumber string

// DeviceSerials is a slice of DeviceSerialNumbers
type DeviceSerials []DeviceSerialNumber

// DeviceSerialNumberEmbedded0 is a string
type DeviceSerialNumberEmbedded0 string

// Macaddr is a string
type Macaddr string

// NonEmptyString is a string
type NonEmptyString string

// EmailAddress is a string
type EmailAddress string

// NewDeviceLinks takes a list of strings and returns them as a single
// DeviceLinks slice
func NewDeviceLinks(uris ...string) DeviceLinks {
links := []Link{}
for _, u := range uris {
links = append(links, Link(u))
}
return DeviceLinks{links}
}

// IntOrStringyInt is an integer that may be presented as a json string
type IntOrStringyInt interface{}

// DiskSizeItem is an int
type DiskSizeItem int

// DeviceHealth is a string
// corresponds to device_health_enum in the database
type DeviceHealth string

// DevicePhase corresponds to device_phase_enum in the database (also used for racks)
type DevicePhase string

// NonNegativeInteger is an int
type NonNegativeInteger int

// PositiveInteger is an int
type PositiveInteger int

// Ipaddr is a string
type Ipaddr string
Loading

0 comments on commit 4144095

Please sign in to comment.