Example #1
0
//main handles the flow of the CLI tool.
func main() {
	//Create basic cli tool
	app := cli.NewApp()
	app.Name = "appfac"
	app.Usage = "CLI Tool for WSO2 Appfactory"

	if len(os.Args) > 1 && os.Args[1] == command.SetBaseUrlCommand {
		setBaseUrl()
	}

	baseUrl := readBaseUrl()
	urls.SetBaseUrl(baseUrl)
	cmdFactory := command.NewFactory()
	var continueFlag bool
	var sessionObject session.Session
	var args []string

	//command `appfac` without argument or help (h) flag
	if len(os.Args) == 1 || os.Args[1] == "help" || os.Args[1] == "h" {
		help.ToolHelp(cmdFactory)
	} else if _command, ok := cmdFactory.CheckIfCommandExists(os.Args[1]); ok {
		args = os.Args[1:]
		if _command == command.SetBaseUrlCommand {
			return
		} else if _command != loginCommand {
			//If session file exists
			if _, err := os.Stat(sessionFilename); err == nil {
				//Read session data
				data, err := ioutil.ReadFile(sessionFilename)
				if err != nil {
					panic(err)
				}
				//Get session data into a session object
				err = json.Unmarshal(data, &sessionObject)
				if err != nil {
					fmt.Println("Error occured while getting stored session.")
				}
				continueFlag = runCommand(_command, args, sessionObject, cmdFactory)
			} else {
				fmt.Println("You must be logged into continue.")
				sessionObject = session.NewSession()
				continueFlag = runCommand(loginCommand, args, sessionObject, cmdFactory)
			}
		} else {
			sessionObject = session.NewSession()
			continueFlag = runCommand(_command, args, sessionObject, cmdFactory)
		}
		for !continueFlag {
			continueFlag = runCommand(loginCommand, args, sessionObject, cmdFactory)
		}
	} else {
		fmt.Println("'" + os.Args[1] + "' is not a valid command. See 'appfac help'")
	}

}
Example #2
0
/* getTenantDomain extracts tenant domain from username and returns it*/
func getTenantDomain() string {
	//Read username from session file and split the tenant domain
	//If session file exists
	filename := "session.txt"
	sessionObject := session.NewSession()
	if _, err := os.Stat(filename); err == nil {
		//Read session data
		data, err := ioutil.ReadFile(filename)
		if err != nil {
			panic(err)
		}
		//Get session data into a session object
		err = json.Unmarshal(data, &sessionObject)
		if err != nil {
			fmt.Println("Error occured while getting stored session.")
		}
		arrays := strings.Split(sessionObject.UserName, "@")
		return arrays[len(arrays)-1]
	}
	return ""
}