Exemplo n.º 1
0
//getFlagValues returns the list of requirements needed to run a command.
func getFlagValues(command command.Command, cmdFlags []string, sessionObject session.Session, args []string) []string {
	var i = 0
	//If flag parsing is disabled
	if command.Metadata().SkipFlagParsing {
		flags := command.Metadata().Flags
		// Prompt for values for each flag
		flagValues := make([]string, len(flags), len(flags))
		for n := 0; n < len(flags); n++ {
			if flag, ok := flags[n].(cli.StringFlag); ok {
				if flag.Usage != "password" {
					fmt.Print(flag.Usage + " > ")
					fmt.Scanf("%s", &flagValues[i])
					if flag.Usage == "userName" {
						sessionObject.UserName = flagValues[i]
					}
					i++
				} else {
					flagValues[i] = password.AskForPassword("Password")
					i++
				}
			}
		}
		return flagValues
	} else {
		isMatch, flagValues := matchArgAndFlags(cmdFlags, args[1:], sessionObject)
		if isMatch {
			return flagValues
		} else {
			return nil
		}
	}

}
Exemplo n.º 2
0
func runCommand(commandName string, args []string, sessionObject session.Session, cmdFactory command.CommandFactory) bool {
	command := cmdFactory.CmdsByName[commandName]
	cmdFlags := cmdFactory.GetCommandFlags(command)
	flagValues := getFlagValues(command, cmdFlags, sessionObject, args)
	configs := cmdFactory.GetCommandConfigs(command, flagValues)
	if configs.Url == "" && configs.Query == "" && configs.Cookie == "" {
		help.HelpTemplate(cmdFactory.CmdsByName[commandName].Metadata())
		return true
	}
	continueFlag, cookie := command.Run(configs)
	if commandName == loginCommand && continueFlag {
		//set session object username
		sessionObject = setSessionUserName(cmdFlags, flagValues)
		sessionObject.Cookie = cookie
		success := writeSession(sessionObject)
		if success {
			fmt.Println("Your session details are stored.")
		} else {
			fmt.Println("Error occured while storing session!")
		}
	}
	return continueFlag
}