Beispiel #1
0
func runShell(cmd *cobra.Command, args []string) {
	var l *login.LoginInfo
	var a app.App

	// Check if you are logged in first
	if l = login.SavedLoginInfo(); l == nil {
		color.Red("You are not logged in. To login, type \"nestor login\"\n")
		os.Exit(1)
	}

	// Check if you have a valid nestor.json file
	nestorJsonPath, err := pathToNestorJson(args)
	if err != nil {
		color.Red("Could not find nestor.json in the path specified\n")
		os.Exit(1)
	}

	a.ManifestPath = nestorJsonPath

	err = a.ParseManifest()
	if err != nil {
		color.Red("%s\n", err.Error())
		os.Exit(1)
	}

	// Check if existing app exists and if so, then we should be making calls to the "UPDATE" function
	// We are ignoring the error for now but at some point we will have to show an error that is not annoying
	err = a.Hydrate(l)
	if err != nil {
		color.Red("Error fetching details for power\n")
	}

	if a.Id == 0 {
		color.Red("You haven't saved your power yet. Run `nestor save` before you can test your power\n")
		os.Exit(1)
	}

	ok := false

	line := liner.NewLiner()

	line.SetCtrlCAborts(true)
	hf, err := os.OpenFile(historyFile, os.O_RDWR, 0644)
	if err != nil {
		color.Red("Unexpected error opening shell\n")
		line.Close()
		os.Exit(1)
	}

	line.ReadHistory(hf)

	for !ok {
		if command, err := line.Prompt("nestor> "); err == nil {
			command = strings.TrimSpace(command)

			if command != "" {
				line.AppendHistory(command)
			}
			switch {
			case quitPattern.MatchString(command):
				fmt.Println("Goodbye!")
				saveHistory(line, hf)
				os.Exit(1)
			case setEnvPattern.MatchString(command):
				matches := setEnvPattern.FindAllStringSubmatch(command, -1)
				resp, err := a.UpdateEnv(l, matches[0][1], matches[0][2])
				if err != nil {
					color.Red("There was an error setting environment variable %s for your power", matches[0][1])
				} else {
					fmt.Printf("Set %s to %s\n", matches[0][1], resp)
				}
			case getEnvPattern.MatchString(command):
				matches := getEnvPattern.FindAllStringSubmatch(command, -1)
				table, err := a.GetEnv(l, matches[0][1])
				if err != nil {
					color.Red("There was an error getting environment variable %s for your power", matches[0][1])
				} else {
					table.Render()
					fmt.Printf("\n")
				}
			case command == "":
				continue
			default:
				output := exec.Output{}
				err := output.Exec(&a, l, command)
				if err != nil {
					color.Red("unexpected error while running your power. Please try again later or contact [email protected]\n", err)
				}
				if output.Logs != "" {
					color.Yellow(output.Logs)
				}
				if len(output.ToSuggest) > 0 {
					suggestion := output.ToSuggest[0]
					fmt.Println("Oops, did you mean `" + suggestion + "`?")
				} else {
					for _, send := range output.ToSend {
						fmt.Println(send.ToString())
					}
				}
			}
		}
	}

	saveHistory(line, hf)
}