// Run executes CLI and return its exit code
Run executes CLI and return its exit code
// Run executes CLI and return its exit code
func (c *CLI) Run(args []string) int {
	var githubTag latest.GithubTag
	flags := flag.NewFlagSet(Name, flag.ExitOnError)
	flags.Usage = func() { fmt.Fprintf(c.errStream, helpText) }
	flags.SetOutput(c.errStream)
	flags.StringVar(&githubTag.Repository,
		"repo", "", "Repository name")
	flags.StringVar(&githubTag.Owner,
		"owner", "", "Repository owner name")
	flgNew := flags.Bool("new",
		false, "Check TAG(VERSION) is new and greater")
	flgFixVerStrFunc := flags.String("fix",
		"none", "Specify FixVersionStrFunc")
	flgVersion := flags.Bool("version",
		false, "Print version information")
	flgHelp := flags.Bool("help",
		false, "Print this message and quit")
	flgDebug := flags.Bool("debug",
		false, "Print verbose(debug) output")
	if err := flags.Parse(args[1:]); err != nil {
		fmt.Fprint(c.errStream, "Failed to parse flag\n")
		return 1
	}
	if *flgVersion {
		fmt.Fprintf(c.errStream, "%s Version v%s build %s\n", Name, Version, GitCommit)
		return 0
	}
	if *flgHelp {
		fmt.Fprintf(c.errStream, helpText)
		return 0
	}
	if os.Getenv(envDebug) != "" {
		*flgDebug = true
	}
	parsedArgs := flags.Args()
	if len(parsedArgs) != 1 {
		fmt.Fprintf(c.errStream, "Invalid arguments\n")
		return 1
	}
	target := parsedArgs[0]
	var f latest.FixVersionStrFunc
	switch *flgFixVerStrFunc {
	case "none":
		f = nil
	case "frontv":
		f = latest.DeleteFrontV()
		target = f(target)
	default:
		fmt.Fprintf(c.errStream, "Invalid fix func: %s\n", *flgFixVerStrFunc)
		return 1
	}
	githubTag.FixVersionStrFunc = f
	res, err := latest.Check(&githubTag, target)
	if err != nil {
		fmt.Fprintf(c.errStream, "Failed to check: %s\n", err.Error())
		return 1
	}
	exitCode := 0
	output := fmt.Sprintf("%s is latest\n", target)
	if *flgNew {
		if !res.New {
			exitCode = 1
			output = fmt.Sprintf("%s is not new\n", target)
		} else {
			output = fmt.Sprintf("%s is new\n", target)
		}
	} else {
		if !res.Latest {
			exitCode = 1
			output = fmt.Sprintf("%s is not latest\n", target)
		}
	}
	if *flgDebug {
		fmt.Fprint(c.outStream, output)
	}
	return exitCode
}
func ( c * CLI ) Run ( args [ ] string ) int { var githubTag latest . GithubTag flags := flag . NewFlagSet ( Name , flag . ExitOnError ) flags . Usage = func ( ) { fmt . Fprintf ( c . errStream , helpText ) } flags . SetOutput ( c . errStream ) flags . StringVar ( & githubTag . Repository , " " , " " , " " ) flags . StringVar ( & githubTag . Owner , " " , " " , " " ) flgNew := flags . Bool ( " " , false , " " ) flgFixVerStrFunc := flags . String ( " " , " " , " " ) flgVersion := flags . Bool ( " " , false , " " ) flgHelp := flags . Bool ( " " , false , " " ) flgDebug := flags . Bool ( " " , false , " " ) if err := flags . Parse ( args [ 1 : ] ) ; err != nil { fmt . Fprint ( c . errStream , " \n " ) return 1 } if * flgVersion { fmt . Fprintf ( c . errStream , " \n " , Name , Version , GitCommit ) return 0 } if * flgHelp { fmt . Fprintf ( c . errStream , helpText ) return 0 } if os . Getenv ( envDebug ) != " " { * flgDebug = true } parsedArgs := flags . Args ( ) if len ( parsedArgs ) != 1 { fmt . Fprintf ( c . errStream , " \n " ) return 1 } target := parsedArgs [ 0 ] var f latest . FixVersionStrFunc switch * flgFixVerStrFunc { case " " : f = nil case " " : f = latest . DeleteFrontV ( ) target = f ( target ) default : fmt . Fprintf ( c . errStream , " \n " , * flgFixVerStrFunc ) return 1 } githubTag . FixVersionStrFunc = f res , err := latest . Check ( & githubTag , target ) if err != nil { fmt . Fprintf ( c . errStream , " \n " , err . Error ( ) ) return 1 } exitCode := 0 output := fmt . Sprintf ( " \n " , target ) if * flgNew { if ! res . New { exitCode = 1 output = fmt . Sprintf ( " \n " , target ) } else { output = fmt . Sprintf ( " \n " , target ) } } else { if ! res . Latest { exitCode = 1 output = fmt . Sprintf ( " \n " , target ) } } if * flgDebug { fmt . Fprint ( c . outStream , output ) } return exitCode }
Run executes CLI and return its exit code
Run executes CLI and return its exit code
// Show version and quit // Show help and quit // Run as debug mode // Specify FixVersionStrFunc // e.g., if version is v0.3.1 it should be 0.3.1 (SemVer format) // Default variables // Check version is `new`