示例#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)
}
示例#2
0
func runSave(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")
	}

	color.Green("+ Building deployment artifact...\n")
	err = a.BuildArtifact()
	if err != nil {
		color.Red("- Error while building deployment artifact for your power\n")
	}

	// Check if you need to do coffee compilation
	err = a.CompileCoffeescript()
	if err != nil {
		color.Red("- There was an error compiling coffeescript in your power\n")
		os.Exit(1)
	}

	err = a.CalculateLocalSha256()
	if err != nil {
		color.Red("- There was an error calculating whether your power needs to be uploaded\n")
		os.Exit(1)
	}

	if a.LocalSha256 != a.RemoteSha256 {
		color.Green("+ Generating zip...\n")
		zip, err := a.ZipBytes()
		if err != nil {
			color.Red("- Error creating a zip of your power's deployment artifact\n")
			os.Exit(1)
		}

		color.Green("+ Uploading zip...\n")
		// Upload app contents
		buffer := bytes.NewBuffer(zip)
		err = a.Upload(buffer, l)
		if err != nil {
			color.Red("- Error while uploading deployment artifact: %+v\n", err)
			os.Exit(1)
		}
	}

	// Make API call to Nestor with contents from JSON file along with S3 URL so that the API can create a functioning bot app
	color.Green("+ Saving power to Nestor...\n")
	err = a.SaveToNestor(l)
	if err != nil {
		color.Red("- Error while saving power to nestor: %+v\n", err)
		os.Exit(1)
	}

	color.Green("+ Successfully saved power to Nestor!\n")

	fmt.Printf("\nYou can test your power by running `nestor shell`\n")
	fmt.Printf("To deploy your power to Slack, run `nestor deploy --latest`\n")
}
示例#3
0
func runDeploy(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 this power\n")
	}

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

	versions, err := version.FetchVersions(a, l)
	if err != nil {
		color.Red("Error fetching versions for your power\n")
		os.Exit(1)
	}

	if latestDeploy {
		pickedVersion := versions[0]
		if pickedVersion.CurrentlyDeployed {
			color.Green("Version %s already deployed. Nothing to do here\n", pickedVersion.Ref)
		} else {
			err = pickedVersion.Deploy(a, l)

			if err != nil {
				color.Red(err.Error())
				os.Exit(1)
			}

			color.Green("Deployed version %s successfully\n", pickedVersion.Ref)
		}
	} else {
		table := version.TableizeVersions(versions)
		fmt.Printf("\n")
		table.Render()
		fmt.Printf("\n")

		ok := false
		intIndex := 0

		for !ok {
			index, promptErr := prompt.Basic(fmt.Sprintf("Pick a version to deploy (1-%d): ", len(versions)), true)
			if promptErr != nil {
				os.Exit(1)
			}

			intIndex, err = strconv.Atoi(index)
			if err == nil && intIndex > 0 && intIndex <= len(versions) {
				ok = true
			}
		}

		pickedVersion := versions[intIndex-1]
		if pickedVersion.CurrentlyDeployed {
			color.Green("Version %s already deployed. Nothing to do here\n", pickedVersion.Ref)
		} else {
			err = pickedVersion.Deploy(a, l)

			if err != nil {
				color.Red("Error deploying %s. Please try again later or contact [email protected]\n", pickedVersion.Ref)
				os.Exit(1)
			}

			color.Green("Deployed version %s successfully\n", pickedVersion.Ref)
		}
	}
}