mirror of https://github.com/knative/func.git
This commit is contained in:
parent
155b6f158f
commit
e1e33370d3
|
@ -34,7 +34,7 @@ type ClientConfig struct {
|
|||
// for use by commands.
|
||||
// See the NewClient constructor which is the fully populated ClientFactory used
|
||||
// by commands by default.
|
||||
// See NewClientFactory which constructs a minimal CientFactory for use
|
||||
// See NewClientFactory which constructs a minimal ClientFactory for use
|
||||
// during testing.
|
||||
type ClientFactory func(ClientConfig, ...fn.Option) (*fn.Client, func())
|
||||
|
||||
|
@ -52,7 +52,7 @@ func NewClientFactory(n func() *fn.Client) ClientFactory {
|
|||
// NewClient constructs an fn.Client with the majority of
|
||||
// the concrete implementations set. Provide additional Options to this constructor
|
||||
// to override or augment as needed, or override the ClientFactory passed to
|
||||
// commands entirely to mock for testing. Note the reutrned cleanup function.
|
||||
// commands entirely to mock for testing. Note the returned cleanup function.
|
||||
// 'Namespace' is optional. If not provided (see DefaultNamespace commentary),
|
||||
// the currently configured is used.
|
||||
// 'Verbose' indicates the system should write out a higher amount of logging.
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"text/tabwriter"
|
||||
|
@ -14,6 +15,9 @@ import (
|
|||
fn "knative.dev/kn-plugin-func"
|
||||
)
|
||||
|
||||
// ErrTemplateRepoDoesNotExist is a sentinel error if a template repository responds with 404 status code
|
||||
var ErrTemplateRepoDoesNotExist = errors.New("template repo does not exist")
|
||||
|
||||
func NewTemplatesCmd(newClient ClientFactory) *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "templates",
|
||||
|
@ -60,7 +64,7 @@ EXAMPLES
|
|||
PreRunE: bindEnv("json", "repository"),
|
||||
}
|
||||
|
||||
cmd.Flags().BoolP("json", "", false, "Set output to JSON format. (Env: $FUNC_JSON)")
|
||||
cmd.Flags().Bool("json", false, "Set output to JSON format. (Env: $FUNC_JSON)")
|
||||
cmd.Flags().StringP("repository", "r", "", "URI to a specific repository to consider (Env: $FUNC_REPOSITORY)")
|
||||
|
||||
cmd.SetHelpFunc(defaultTemplatedHelp)
|
||||
|
@ -79,13 +83,25 @@ func runTemplates(cmd *cobra.Command, args []string, newClient ClientFactory) (e
|
|||
return
|
||||
}
|
||||
|
||||
// Simple ping to the repo to avoid subsequent errors from http package if it does not exist
|
||||
if cfg.Repository != "" {
|
||||
res, err := http.Get(cfg.Repository)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
if res.StatusCode == http.StatusNotFound {
|
||||
return ErrTemplateRepoDoesNotExist
|
||||
}
|
||||
}
|
||||
|
||||
// Client which will provide data
|
||||
client, done := newClient(ClientConfig{Verbose: cfg.Verbose},
|
||||
fn.WithRepository(cfg.Repository), // Use exactly this repo OR
|
||||
fn.WithRepositoriesPath(cfg.RepositoriesPath)) // Path on disk to installed repos
|
||||
defer done()
|
||||
|
||||
// For a singl language runtime
|
||||
// For a single language runtime
|
||||
// -------------------
|
||||
if len(args) == 1 {
|
||||
templates, err := client.Templates().List(args[0])
|
||||
|
@ -105,7 +121,7 @@ func runTemplates(cmd *cobra.Command, args []string, newClient ClientFactory) (e
|
|||
}
|
||||
return nil
|
||||
} else if len(args) > 1 {
|
||||
return errors.New("unexpected extra arguments.")
|
||||
return errors.New("unexpected extra arguments")
|
||||
}
|
||||
|
||||
// All language runtimes
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"gotest.tools/v3/assert"
|
||||
|
||||
fn "knative.dev/kn-plugin-func"
|
||||
. "knative.dev/kn-plugin-func/testing"
|
||||
)
|
||||
|
@ -140,3 +143,24 @@ http`
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
func TestTemplates_ErrTemplateRepoDoesNotExist(t *testing.T) {
|
||||
defer t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
||||
cmd := NewTemplatesCmd(NewClientFactory(func() *fn.Client {
|
||||
return fn.New()
|
||||
}))
|
||||
cmd.SetArgs([]string{"--repository", "https://github.com/boson-project/repo-does-not-exist"})
|
||||
err := cmd.Execute()
|
||||
assert.Assert(t, err != nil)
|
||||
assert.Assert(t, errors.Is(err, ErrTemplateRepoDoesNotExist))
|
||||
}
|
||||
|
||||
func TestTemplates_WrongRepositoryUrl(t *testing.T) {
|
||||
defer t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
||||
cmd := NewTemplatesCmd(NewClientFactory(func() *fn.Client {
|
||||
return fn.New()
|
||||
}))
|
||||
cmd.SetArgs([]string{"--repository", "wrong://github.com/boson-project/repo-does-not-exist"})
|
||||
err := cmd.Execute()
|
||||
assert.Assert(t, err != nil)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue