Exemple #1
0
func TestHostChange(t *testing.T) {
	f, err := ioutil.TempFile("", "configtest")
	if err != nil {
		t.Fatal(err)
	}
	defer f.Close()

	conf := &command.Config{
		Path: f.Name(),
	}

	// Remove the file to test the scenario in which it doesn't exist
	os.Remove(f.Name())

	ui := new(cli.MockUi)

	c := &command.ConfCommand{
		Ui:     ui,
		Config: conf,
	}

	newHost := "0.0.0.0:8000"

	ui.InputReader = bytes.NewBufferString(newHost + "\n")

	c.Run([]string{"host", "edit"})

	writtenConf, err := command.ParseConfigFile(conf.Path)
	if err != nil {
		t.Fatalf("ParseConfigFile: %s", err)
	}

	if writtenConf.Host != newHost {
		t.Fatalf("Host should be %s, not %s", newHost, writtenConf.Host)
	}

	os.Remove(writtenConf.Path)
}
Exemple #2
0
func init() {
	UI = &cli.BasicUi{Writer: os.Stdout, Reader: os.Stdin}

	user, err := user.Current()
	if err != nil {
		UI.Error(err.Error())
		os.Exit(1)
	}

	configPath := path.Join(user.HomeDir, command.ConfigFileName)

	c, err := command.ParseConfigFile(configPath)
	if err != nil {
		UI.Error(err.Error())
		os.Exit(1)
	}

	Configuration = c

	var db olddata.DB
	var databaseError error

	if Configuration.DirectDB {
		if Configuration.DB != "" {
			db, databaseError = models.MongoDB(Configuration.DB)
		} else {
			databaseError = fmt.Errorf("No database listed")
		}
	} else {
		db = &gaia.DB{
			URL:      Configuration.Host,
			Username: Configuration.PublicCredential,
			Password: Configuration.PrivateCredential,
			Client:   new(http.Client),
		}
	}
	conn, err := grpc.Dial(
		"elos.pw:4444",
		grpc.WithPerRPCCredentials(
			auth.RawCredentials(
				Configuration.Credential.Public,
				Configuration.Credential.Private,
			),
		),
		grpc.WithInsecure(),
	)
	if err != nil {
		log.Fatal(err)
	}
	// don't close connection because we'd lose connection should
	// move declaration of dbc higher in scope TODO(nclandolfi)
	dbc := data.NewDBClient(conn)

	Commands = map[string]cli.CommandFactory{
		"habit": func() (cli.Command, error) {
			return &command.HabitCommand{
				UI:     UI,
				UserID: Configuration.UserID,
				DB:     db,
			}, databaseError
		},
		"people": func() (cli.Command, error) {
			return &command.PeopleCommand{
				UI:     UI,
				UserID: Configuration.UserID,
				DB:     db,
			}, databaseError
		},
		"setup": func() (cli.Command, error) {
			return &command.SetupCommand{
				UI:     UI,
				Config: Configuration,
			}, nil
		},
		"tag": func() (cli.Command, error) {
			return &command.TagCommand{
				UI:     UI,
				UserID: Configuration.UserID,
				DB:     db,
			}, databaseError
		},
		"stream": func() (cli.Command, error) {
			return &command.StreamCommand{
				UI:     UI,
				UserID: Configuration.UserID,
				DB:     db,
			}, databaseError
		},
		"todo": func() (cli.Command, error) {
			return &command.TodoCommand{
				UI:     UI,
				UserID: Configuration.Credential.OwnerID,
				DB:     data.DB(dbc),
			}, databaseError
		},
		"cal2": func() (cli.Command, error) {
			return &command.Cal2Command{
				UI:       UI,
				UserID:   Configuration.Credential.OwnerID,
				DBClient: dbc,
			}, nil
		},
		"records": func() (cli.Command, error) {
			return &command.RecordsCommand{
				UI:       UI,
				UserID:   Configuration.Credential.OwnerID,
				DBClient: dbc,
			}, nil
		},
	}
}