コード例 #1
0
ファイル: main_test.go プロジェクト: swhitley/parse-cli
func TestNewClientInvalidServerURL(t *testing.T) {
	t.Parallel()
	c, err := parsecli.NewParseAPIClient(&parsecli.Env{Server: ":"})
	ensure.True(t, c == nil)
	ensure.Err(t, err, regexp.MustCompile("invalid server URL"))
}
コード例 #2
0
ファイル: main.go プロジェクト: jinkawin/parse-cli
func main() {
	// some parts of apps.go are unable to handle
	// interrupts, this logic ensures we exit on system interrupts
	interrupt := make(chan os.Signal, 1)
	signal.Notify(interrupt, os.Interrupt)
	go func() {
		<-interrupt
		os.Exit(1)
	}()

	e := parsecli.Env{
		Root:        os.Getenv("PARSE_ROOT"),
		Server:      os.Getenv("PARSE_SERVER"),
		ErrorStack:  os.Getenv("PARSE_ERROR_STACK") == "1",
		ParserEmail: os.Getenv("PARSER_EMAIL"),
		Out:         os.Stdout,
		Err:         os.Stderr,
		In:          os.Stdin,
		Exit:        os.Exit,
		Clock:       clock.New(),
	}
	if e.Root == "" {
		cur, err := os.Getwd()
		if err != nil {
			fmt.Fprintf(e.Err, "Failed to get current directory:\n%s\n", err)
			os.Exit(1)
		}
		root := parsecli.GetProjectRoot(&e, cur)
		if parsecli.IsProjectDir(root) {
			e.Root = root
			config, err := parsecli.ConfigFromDir(root)
			if err != nil {
				fmt.Fprintln(e.Err, err)
				os.Exit(1)
			}
			e.Type = config.GetProjectConfig().Type
			if e.ParserEmail == "" {
				e.ParserEmail = config.GetProjectConfig().ParserEmail
			}
		} else {
			e.Type = parsecli.LegacyParseFormat
			e.Root = parsecli.GetLegacyProjectRoot(&e, cur)
		}
	}
	if e.Type != parsecli.LegacyParseFormat && e.Type != parsecli.ParseFormat && e.Type != parsecli.HerokuFormat {
		fmt.Fprintf(e.Err, "Unknown project type %d.\n", e.Type)
		os.Exit(1)
	}

	if e.Server == "" {
		e.Server = parsecli.DefaultBaseURL
	}

	apiClient, err := parsecli.NewParseAPIClient(&e)
	if err != nil {
		fmt.Fprintln(e.Err, err)
		os.Exit(1)
	}
	e.ParseAPIClient = apiClient
	if e.Type == parsecli.HerokuFormat {
		apiClient, err := parsecli.NewHerokuAPIClient(&e)
		if err != nil {
			fmt.Fprintln(e.Err, err)
			os.Exit(1)
		}
		e.HerokuAPIClient = apiClient
	}

	var (
		rootCmd *cobra.Command
		command []string
	)
	switch e.Type {
	case parsecli.LegacyParseFormat, parsecli.ParseFormat:
		command, rootCmd = parseRootCmd(&e)
	case parsecli.HerokuFormat:
		command, rootCmd = herokuRootCmd(&e)
	}

	if len(command) == 0 || command[0] != "update" {
		message, err := checkIfSupported(&e, parsecli.Version, command...)
		if err != nil {
			fmt.Fprintln(e.Err, err)
			os.Exit(1)
		}
		if message != "" {
			fmt.Fprintln(e.Err, message)
		}
	}

	if err := rootCmd.Execute(); err != nil {
		// Error is already printed in Execute()
		os.Exit(1)
	}
}