Esempio n. 1
0
File: demo.go Progetto: rwz/cli
// Demo returns one problem for each active track.
func Demo(ctx *cli.Context) {
	c, err := config.New(ctx.GlobalString("config"))
	if err != nil {
		log.Fatal(err)
	}

	client := api.NewClient(c)

	problems, err := client.Demo()
	if err != nil {
		log.Fatal(err)
	}

	if dirOpt := ctx.String("dir"); dirOpt != "" {
		c.Dir = paths.Exercises(dirOpt)
	}

	fmt.Printf("Your exercises will be saved at: %s\n", c.Dir)
	hw := user.NewHomework(problems, c)
	if err := hw.Save(); err != nil {
		log.Fatal(err)
	}

	hw.Report(user.HWAll)

	fmt.Println("Next step: choose a language, read the README, and make the test suite pass.")
}
Esempio n. 2
0
func (c *Config) setDefaults() error {
	if c.API == "" {
		c.API = hostAPI
	}

	if c.XAPI == "" {
		c.XAPI = hostXAPI
	}

	c.Dir = paths.Exercises(c.Dir)

	return nil
}
Esempio n. 3
0
// Update sets new values where given.
func (c *Config) Update(key, host, dir, xapi string) error {
	key = strings.TrimSpace(key)
	if key != "" {
		c.APIKey = key
	}

	host = strings.TrimSpace(host)
	if host != "" {
		c.API = host
	}

	if dir != "" {
		c.Dir = paths.Exercises(dir)
	}

	xapi = strings.TrimSpace(xapi)
	if xapi != "" {
		c.XAPI = xapi
	}

	return nil
}
Esempio n. 4
0
func TestLoad(t *testing.T) {
	tmpDir, err := ioutil.TempDir("", "")
	if err != nil {
		t.Fatal(err)
	}
	configPath := filepath.Join(tmpDir, "config.json")
	if err := os.Link(fixturePath(t, "config.json"), configPath); err != nil {
		t.Fatal(err)
	}
	dirtyPath := filepath.Join(tmpDir, "dirty.json")
	if err := os.Link(fixturePath(t, "dirty.json"), dirtyPath); err != nil {
		t.Fatal(err)
	}
	paths.Home = tmpDir

	testCases := []struct {
		desc                string
		in                  string // the name of the file passed as a command line argument
		out                 string // the name of the file that the config will be written to
		dir, key, api, xapi string // the actual config values
	}{
		{
			desc: "defaults",
			in:   "",
			out:  paths.Config(""),
			dir:  paths.Exercises(""),
			key:  "",
			api:  hostAPI,
			xapi: hostXAPI,
		},
		{
			desc: "file exists",
			in:   configPath,
			out:  configPath,
			dir:  "/a/b/c",
			key:  "abc123",
			api:  "http://api.example.com",
			xapi: "http://x.example.com",
		},
		{
			desc: "unexpanded path",
			in:   "~/config.json",
			out:  configPath,
			dir:  "/a/b/c",
			key:  "abc123",
			api:  "http://api.example.com",
			xapi: "http://x.example.com",
		},
		{
			desc: "sanitizes whitespace",
			in:   "~/dirty.json",
			out:  filepath.Join(tmpDir, "dirty.json"),
			dir:  "/a/b/c",
			key:  "abc123",
			api:  "http://api.example.com",
			xapi: "http://x.example.com",
		},
	}

	for _, tc := range testCases {
		c, err := New(tc.in)
		if err != nil {
			t.Fatal(err)
		}
		assert.Equal(t, tc.out, c.File, tc.desc)
		assert.Equal(t, tc.dir, c.Dir, tc.desc)
		assert.Equal(t, tc.key, c.APIKey, tc.desc)
		assert.Equal(t, tc.api, c.API, tc.desc)
		assert.Equal(t, tc.xapi, c.XAPI, tc.desc)
	}
}