diff --git a/README.md b/README.md index 747b9a8..7501ca4 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Website | Community | Blog

- User Manual | + User Manual | API Reference

Get Early Access @@ -49,6 +49,12 @@ To upgrade: brew upgrade instill-ai/tap/instill ``` +## Usage + +``` + +``` + ## Issues and discussions Please directly report any issues in [Issues](https://github.com/instill-ai/cli/issues) or [Pull requests](https://github.com/instill-ai/cli/pulls), or raise a topic in [Discussions](https://github.com/instill-ai/cli/discussions). diff --git a/internal/config/stub.go b/internal/config/stub.go index 9a39fca..50b9ce7 100644 --- a/internal/config/stub.go +++ b/internal/config/stub.go @@ -2,6 +2,7 @@ package config import ( "errors" + "github.com/instill-ai/cli/internal/instance" ) type ConfigStub map[string]string @@ -53,3 +54,31 @@ func (c ConfigStub) DefaultHost() (string, error) { func (c ConfigStub) DefaultHostWithSource() (string, string, error) { return "", "", nil } + +func (c ConfigStub) DefaultHostname() string { + return instance.FallbackHostname() +} + +func (c ConfigStub) HostsTyped() ([]HostConfigTyped, error) { + ins := []HostConfigTyped{ + { + APIHostname: "api.instill.tech", + IsDefault: true, + APIVersion: "v1alpha", + Oauth2Hostname: "auth.instill.tech", + Oauth2Audience: "https://api.instill.tech", + Oauth2Issuer: "https://auth.instill.tech/", + Oauth2Secret: "foobar", + Oauth2ClientID: "barfoo", + }, + } + return ins, nil +} + +func (c ConfigStub) SaveTyped(*HostConfigTyped) error { + return nil +} + +func ConfigStubFactory() (Config, error) { + return ConfigStub{}, nil +} diff --git a/pkg/cmd/api/api.go b/pkg/cmd/api/api.go index d79cc80..8f3634d 100644 --- a/pkg/cmd/api/api.go +++ b/pkg/cmd/api/api.go @@ -160,6 +160,39 @@ func apiRun(opts *ApiOptions) error { return err } + // get the host config + cfg, err := opts.Config() + if err != nil { + return err + } + var host *config.HostConfigTyped + if err != nil { + return err + } + hosts, err := cfg.HostsTyped() + if err != nil { + return err + } + hostname := opts.Hostname + if hostname == "" { + hostname = cfg.DefaultHostname() + } + for i := range hosts { + if hosts[i].APIHostname == hostname { + host = &hosts[i] + break + } + } + if host == nil { + return fmt.Errorf(heredoc.Docf( + `ERROR: instance '%s' does not exist + + You can add it with: + $ inst instances add %s`, + hostname, hostname)) + } + + // set up the http client method := opts.RequestMethod requestPath := opts.RequestPath requestHeaders := opts.RequestHeaders @@ -201,29 +234,6 @@ func apiRun(opts *ApiOptions) error { defer opts.IO.StopPager() } - // get the host config - cfg, err := opts.Config() - if err != nil { - return err - } - var host *config.HostConfigTyped - if err != nil { - return err - } - hosts, err := cfg.HostsTyped() - if err != nil { - return err - } - for i := range hosts { - if hosts[i].APIHostname == opts.Hostname { - host = &hosts[i] - break - } - } - if host == nil { - return fmt.Errorf("instance '%s' doesn't exist", opts.Hostname) - } - // set up the request // TODO support other services than VDP requestPath = "vdp/" + host.APIVersion + "/" + strings.TrimPrefix(requestPath, "/") diff --git a/pkg/cmd/api/api_test.go b/pkg/cmd/api/api_test.go index 1c5501a..699141c 100644 --- a/pkg/cmd/api/api_test.go +++ b/pkg/cmd/api/api_test.go @@ -23,7 +23,9 @@ import ( ) func Test_NewCmdApi(t *testing.T) { - f := &cmdutil.Factory{} + f := &cmdutil.Factory{ + Config: config.ConfigStubFactory, + } tests := []struct { name string @@ -35,7 +37,7 @@ func Test_NewCmdApi(t *testing.T) { name: "override method", cli: "pipelines -XDELETE", wants: ApiOptions{ - Hostname: "", + Hostname: "api.instill.tech", RequestMethod: "DELETE", RequestMethodPassed: true, RequestPath: "pipelines", @@ -55,7 +57,7 @@ func Test_NewCmdApi(t *testing.T) { name: "with headers", cli: "user -H 'accept: text/plain' -i", wants: ApiOptions{ - Hostname: "", + Hostname: "api.instill.tech", RequestMethod: "GET", RequestMethodPassed: false, RequestPath: "user", @@ -75,7 +77,7 @@ func Test_NewCmdApi(t *testing.T) { name: "with silenced output", cli: "models --silent", wants: ApiOptions{ - Hostname: "", + Hostname: "api.instill.tech", RequestMethod: "GET", RequestMethodPassed: false, RequestPath: "models", @@ -95,7 +97,7 @@ func Test_NewCmdApi(t *testing.T) { name: "with request body from file", cli: "user --input myfile", wants: ApiOptions{ - Hostname: "", + Hostname: "api.instill.tech", RequestMethod: "GET", RequestMethodPassed: false, RequestPath: "user", @@ -120,7 +122,7 @@ func Test_NewCmdApi(t *testing.T) { name: "with cache", cli: "user --cache 5m", wants: ApiOptions{ - Hostname: "", + Hostname: "api.instill.tech", RequestMethod: "GET", RequestMethodPassed: false, RequestPath: "user", @@ -140,7 +142,7 @@ func Test_NewCmdApi(t *testing.T) { name: "with template", cli: "user -t 'hello {{.name}}'", wants: ApiOptions{ - Hostname: "", + Hostname: "api.instill.tech", RequestMethod: "GET", RequestMethodPassed: false, RequestPath: "user", @@ -160,7 +162,7 @@ func Test_NewCmdApi(t *testing.T) { name: "with jq filter", cli: "user -q .name", wants: ApiOptions{ - Hostname: "", + Hostname: "api.instill.tech", RequestMethod: "GET", RequestMethodPassed: false, RequestPath: "user", @@ -372,7 +374,7 @@ func Test_apiRun(t *testing.T) { stream, _, stdout, stderr := iostreams.Test() tt.options.IO = stream - tt.options.Config = func() (config.Config, error) { return config.NewBlankConfig(), nil } + tt.options.Config = config.ConfigStubFactory tt.options.HTTPClient = func() (*http.Client, error) { var tr roundTripper = func(req *http.Request) (*http.Response, error) { resp := tt.httpResponse @@ -458,9 +460,7 @@ func Test_apiRun_inputFile(t *testing.T) { } return &http.Client{Transport: tr}, nil }, - Config: func() (config.Config, error) { - return config.NewBlankConfig(), nil - }, + Config: config.ConfigStubFactory, } err := apiRun(&options) @@ -469,7 +469,7 @@ func Test_apiRun_inputFile(t *testing.T) { } assert.Equal(t, "POST", resp.Request.Method) - assert.Equal(t, "/hello?a=b&c=d", resp.Request.URL.RequestURI()) + assert.Equal(t, "/vdp/v1alpha/hello?a=b&c=d", resp.Request.URL.RequestURI()) assert.Equal(t, tt.contentLength, resp.Request.ContentLength) assert.Equal(t, "", resp.Request.Header.Get("Content-Type")) assert.Equal(t, tt.inputContents, bodyBytes) @@ -493,9 +493,7 @@ func Test_apiRun_cache(t *testing.T) { } return &http.Client{Transport: tr}, nil }, - Config: func() (config.Config, error) { - return config.NewBlankConfig(), nil - }, + Config: config.ConfigStubFactory, RequestPath: "pipelines", CacheTTL: time.Minute, diff --git a/pkg/cmd/api/http_test.go b/pkg/cmd/api/http_test.go index 545702f..a70d176 100644 --- a/pkg/cmd/api/http_test.go +++ b/pkg/cmd/api/http_test.go @@ -43,7 +43,7 @@ func Test_httpRequest(t *testing.T) { name: "simple GET", args: args{ client: &httpClient, - host: "instill.tech", + host: "api.instill.tech", method: "GET", p: "/models", params: nil, @@ -61,7 +61,7 @@ func Test_httpRequest(t *testing.T) { name: "GET with leading slash", args: args{ client: &httpClient, - host: "instill.tech", + host: "api.instill.tech", method: "GET", p: "/models", params: nil, @@ -79,7 +79,7 @@ func Test_httpRequest(t *testing.T) { name: "GET with params", args: args{ client: &httpClient, - host: "instill.tech", + host: "api.instill.tech", method: "GET", p: "models", params: map[string]interface{}{ @@ -99,7 +99,7 @@ func Test_httpRequest(t *testing.T) { name: "POST with params", args: args{ client: &httpClient, - host: "github.com", + host: "api.github.com", method: "POST", p: "repos", params: map[string]interface{}{ @@ -119,7 +119,7 @@ func Test_httpRequest(t *testing.T) { name: "POST with body and type", args: args{ client: &httpClient, - host: "github.com", + host: "api.github.com", method: "POST", p: "repos", params: bytes.NewBufferString("CUSTOM"), diff --git a/pkg/cmd/auth/login/login_test.go b/pkg/cmd/auth/login/login_test.go index af2162b..088a8df 100644 --- a/pkg/cmd/auth/login/login_test.go +++ b/pkg/cmd/auth/login/login_test.go @@ -2,6 +2,8 @@ package login import ( "bytes" + "github.com/instill-ai/cli/internal/config" + "github.com/instill-ai/cli/internal/instance" "testing" "github.com/google/shlex" @@ -34,7 +36,7 @@ func Test_NewCmdLogin(t *testing.T) { stdinTTY: true, cli: "", wants: LoginOptions{ - Hostname: "", + Hostname: instance.FallbackHostname(), Interactive: true, }, }, @@ -46,6 +48,7 @@ func Test_NewCmdLogin(t *testing.T) { f := &cmdutil.Factory{ IOStreams: io, Executable: func() string { return "/path/to/instill" }, + Config: config.ConfigStubFactory, } io.SetStdoutTTY(true) diff --git a/pkg/cmd/completion/completion_test.go b/pkg/cmd/completion/completion_test.go index 3d9fdee..a8ac64f 100644 --- a/pkg/cmd/completion/completion_test.go +++ b/pkg/cmd/completion/completion_test.go @@ -25,7 +25,7 @@ func TestNewCmdCompletion(t *testing.T) { { name: "zsh completion", args: "completion -s zsh", - wantOut: "#compdef _instill instill", + wantOut: "compdef _instill instill", }, { name: "fish completion", diff --git a/pkg/cmd/factory/default.go b/pkg/cmd/factory/default.go index 6738ab3..3514b3a 100644 --- a/pkg/cmd/factory/default.go +++ b/pkg/cmd/factory/default.go @@ -2,6 +2,7 @@ package factory import ( "errors" + "fmt" "net/http" "os" "path/filepath" @@ -115,17 +116,20 @@ func executable(fallbackName string) string { func configFunc() func() (config.Config, error) { var cachedConfig config.Config - var configError error + var err error return func() (config.Config, error) { - if cachedConfig != nil || configError != nil { - return cachedConfig, configError + if cachedConfig != nil || err != nil { + if err != nil { + fmt.Printf("ERROR: cant read the config\n%s", err) + } + return cachedConfig, err } - cachedConfig, configError = config.ParseDefaultConfig() - if errors.Is(configError, os.ErrNotExist) { + cachedConfig, err = config.ParseDefaultConfig() + if errors.Is(err, os.ErrNotExist) { cachedConfig = config.NewBlankConfig() - configError = nil + err = nil } - return cachedConfig, configError + return cachedConfig, err } } diff --git a/pkg/cmd/factory/http_test.go b/pkg/cmd/factory/http_test.go index 8aa3a97..9189a92 100644 --- a/pkg/cmd/factory/http_test.go +++ b/pkg/cmd/factory/http_test.go @@ -2,6 +2,7 @@ package factory import ( "fmt" + "github.com/instill-ai/cli/internal/config" "net/http" "net/http/httptest" "os" @@ -140,6 +141,10 @@ func TestNewHTTPClient(t *testing.T) { type tinyConfig map[string]string +func (c tinyConfig) SaveTyped(typed *config.HostConfigTyped) error { + return nil +} + func (c tinyConfig) Get(host, key string) (string, error) { return c[fmt.Sprintf("%s:%s", host, key)], nil }