Skip to content

Commit

Permalink
Docker Image (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
leg100 authored Oct 31, 2021
1 parent 6bba7ac commit cef9bed
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 51 deletions.
7 changes: 1 addition & 6 deletions cmd/otf/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@ package main
import (
"fmt"

"github.com/leg100/otf/http"
"github.com/spf13/cobra"
)

const (
DummyToken = "dummy"
)

func LoginCommand(store KVStore) *cobra.Command {
var address string

func LoginCommand(store KVStore, address string) *cobra.Command {
cmd := &cobra.Command{
Use: "login",
Short: "Login to OTF",
Expand All @@ -28,7 +25,5 @@ func LoginCommand(store KVStore) *cobra.Command {
},
}

cmd.Flags().StringVar(&address, "address", http.DefaultAddress, "Address of OTF instance")

return cmd
}
17 changes: 1 addition & 16 deletions cmd/otf/login_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -11,23 +10,9 @@ import (
func TestLoginCommand(t *testing.T) {
var store KVStore = KVMap(make(map[string]string))

cmd := LoginCommand(store)
cmd := LoginCommand(store, "localhost:8080")
require.NoError(t, cmd.Execute())

token, _ := store.Load("localhost:8080")
assert.Equal(t, "dummy", token)
}

func TestLoginCommandWithExplicitAddress(t *testing.T) {
// Ensure env var doesn't interfere with test
os.Unsetenv("OTF_ADDRESS")

var store KVStore = KVMap(make(map[string]string))

cmd := LoginCommand(store)
cmd.SetArgs([]string{"--address", "otf.dev:8080"})
require.NoError(t, cmd.Execute())

token, _ := store.Load("otf.dev:8080")
assert.Equal(t, "dummy", token)
}
16 changes: 13 additions & 3 deletions cmd/otf/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"

cmdutil "github.com/leg100/otf/cmd"
"github.com/leg100/otf/http"
"github.com/spf13/cobra"
)

Expand All @@ -21,21 +22,30 @@ func main() {
}

func Run(ctx context.Context, args []string) error {
cfg, err := http.NewConfig(LoadCredentials)
if err != nil {
return err
}

cmd := &cobra.Command{
Use: "otf",
SilenceUsage: true,
SilenceErrors: true,
Run: func(cmd *cobra.Command, args []string) {},
}

cmd.PersistentFlags().StringVar(&cfg.Address, "address", http.DefaultAddress, "Address of OTF server")

cmd.SetArgs(args)

store, err := NewCredentialsStore()
if err != nil {
return err
}

cmd.AddCommand(LoginCommand(store))
cmd.AddCommand(OrganizationCommand())
cmd.AddCommand(WorkspaceCommand())
cmd.AddCommand(LoginCommand(store, cfg.Address))
cmd.AddCommand(OrganizationCommand(cfg))
cmd.AddCommand(WorkspaceCommand(cfg))

cmdutil.SetFlagsFromEnvVariables(cmd.Flags())

Expand Down
8 changes: 8 additions & 0 deletions cmd/otf/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ func TestMain(t *testing.T) {
name: "login",
args: []string{"login", "-h"},
},
{
name: "login and address",
args: []string{"login", "-h", "--address", "test.abc:1234"},
},
{
name: "address",
args: []string{"--address", "test.abc:1234"},
},
{
name: "organization new",
args: []string{"organizations", "new", "-h"},
Expand Down
13 changes: 2 additions & 11 deletions cmd/otf/organization.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
package main

import (
"os"

"github.com/leg100/otf/http"
"github.com/spf13/cobra"
)

func OrganizationCommand() *cobra.Command {
cfg, err := http.NewConfig(LoadCredentials)
if err != nil {
panic(err.Error())
}

func OrganizationCommand(factory http.ClientFactory) *cobra.Command {
cmd := &cobra.Command{
Use: "organizations",
Short: "Organization management",
}
cmd.Flags().StringVar(&cfg.Address, "address", http.DefaultAddress, "Address of OTF server")
cmd.Flags().StringVar(&cfg.Token, "token", os.Getenv("OTF_TOKEN"), "Authentication token")

cmd.AddCommand(OrganizationNewCommand(cfg))
cmd.AddCommand(OrganizationNewCommand(factory))

return cmd
}
21 changes: 6 additions & 15 deletions cmd/otf/workspace.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,21 @@
package main

import (
"os"

"github.com/leg100/otf/http"
"github.com/spf13/cobra"
)

func WorkspaceCommand() *cobra.Command {
cfg, err := http.NewConfig(LoadCredentials)
if err != nil {
panic(err.Error())
}

func WorkspaceCommand(factory http.ClientFactory) *cobra.Command {
cmd := &cobra.Command{
Use: "workspaces",
Short: "Workspace management",
}
cmd.Flags().StringVar(&cfg.Address, "address", http.DefaultAddress, "Address of OTF server")
cmd.Flags().StringVar(&cfg.Token, "token", os.Getenv("OTF_TOKEN"), "Authentication token")

cmd.AddCommand(WorkspaceListCommand(cfg))
cmd.AddCommand(WorkspaceShowCommand(cfg))
cmd.AddCommand(WorkspaceEditCommand(cfg))
cmd.AddCommand(WorkspaceLockCommand(cfg))
cmd.AddCommand(WorkspaceUnlockCommand(cfg))
cmd.AddCommand(WorkspaceListCommand(factory))
cmd.AddCommand(WorkspaceShowCommand(factory))
cmd.AddCommand(WorkspaceEditCommand(factory))
cmd.AddCommand(WorkspaceLockCommand(factory))
cmd.AddCommand(WorkspaceUnlockCommand(factory))

return cmd
}

0 comments on commit cef9bed

Please sign in to comment.