Ejemplo n.º 1
0
func doRetire(c *cli.Context) error {
	conffile := c.GlobalString("conf")
	force := c.Bool("force")
	argHostIDs := c.Args()

	if len(argHostIDs) < 1 {
		argHostIDs = make([]string, 1)
		if argHostIDs[0] = LoadHostIDFromConfig(conffile); argHostIDs[0] == "" {
			cli.ShowCommandHelp(c, "retire")
			os.Exit(1)
		}
	}

	if !force && !prompter.YN("Retire following hosts.\n  "+strings.Join(argHostIDs, "\n  ")+"\nAre you sure?", true) {
		logger.Log("", "retirement is canceled.")
		return nil
	}

	client := newMackerel(conffile)

	for _, hostID := range argHostIDs {
		err := client.RetireHost(hostID)
		logger.DieIf(err)

		logger.Log("retired", hostID)
	}
	return nil
}
Ejemplo n.º 2
0
// InitConfig initialises a .plzconfig template in the given directory.
func InitConfig(dir string, bazelCompatibility bool) {
	if dir == "." {
		core.FindRepoRoot(false)
		if core.RepoRoot != "" {
			config := path.Join(core.RepoRoot, core.ConfigFileName)
			if !prompter.YN(fmt.Sprintf("You already seem to be in a plz repo (found %s). Continue?", config), false) {
				os.Exit(1)
			}
		}
	}
	dir, err := filepath.Abs(dir)
	if err != nil {
		log.Warning("Can't determine absolute directory: %s", err)
	}
	config := path.Join(dir, core.ConfigFileName)
	contents := fmt.Sprintf(configTemplate, core.PleaseVersion)
	if bazelCompatibility {
		contents += bazelCompatibilityConfig
	}
	if err := ioutil.WriteFile(config, []byte(contents), 0644); err != nil {
		log.Fatalf("Failed to write file: %s", err)
	}
	fmt.Printf("Wrote config template to %s, you're now ready to go!\n", config)
	// Now write the wrapper script
	data := MustAsset(wrapperScriptName)
	if err := ioutil.WriteFile(wrapperScriptName, data, 0755); err != nil {
		log.Fatalf("Failed to write file: %s", err)
	}
	fmt.Printf("\nAlso wrote wrapper script to %s; users can invoke that directly to run Please, even without it installed.\n", wrapperScriptName)
}
Ejemplo n.º 3
0
func ExampleYN() {
	if prompter.YN("Do you like sushi?", true) {
		fmt.Println("y")
		fmt.Print("Nice! Let's go sushi bar!")
	}
	// Output:
	// Do you like sushi? (y/n) [y]: y
	// Nice! Let's go sushi bar!
}
Ejemplo n.º 4
0
func promptForDir(name string, def string) (dir string, err error) {
	dir = prompter.Prompt(name, def)
	dir, err = homedir.Expand(dir)
	if err != nil {
		return
	}
	exists, err := util.DirExists(dir)
	if !exists {
		create := prompter.YN(fmt.Sprintf("%s does not exist.  Create?", dir), false)
		if create {
			err = os.MkdirAll(dir, 0755)
			return
		}
		err = errors.New("Did not create directory")
	}
	return
}
Ejemplo n.º 5
0
func doRemove(c *cli.Context) error {
	target := compileTargetPath(c.Args().Get(0))

	if _, err := os.Stat(target); err == nil {
		if !prompter.YN("Remove? "+target, true) {
			os.Exit(0)
		}

		err := os.RemoveAll(target)
		if err == nil {
			fmt.Println("Removed: " + target)
		} else {
			fmt.Println(err)
		}
	} else {
		fmt.Println("Doesn't exist: " + target)
	}

	return nil
}
Ejemplo n.º 6
0
func doRetire(argv []string) int {
	conf, force, err := resolveConfigForRetire(argv)
	if err != nil {
		return exitStatusError
	}
	if conf.Apikey == "" {
		logger.Criticalf("Apikey must be specified in the command-line flag or in the config file")
		return exitStatusError
	}

	hostID, err := command.LoadHostID(conf.Root)
	if err != nil {
		logger.Warningf("HostID file is not found")
		return exitStatusError
	}

	api, err := mackerel.NewAPI(conf.Apibase, conf.Apikey, conf.Verbose)
	if err != nil {
		logger.Errorf("failed to create api client: %s", err)
		return exitStatusError
	}

	if !force && !prompter.YN(fmt.Sprintf("retire this host? (hostID: %s)", hostID), false) {
		logger.Infof("Retirement is canceled.")
		return exitStatusError
	}

	err = api.RetireHost(hostID)
	if err != nil {
		logger.Errorf("failed to retire the host: %s", err)
		return exitStatusError
	}
	logger.Infof("This host (hostID: %s) has been retired.", hostID)
	// just to try to remove hostID file.
	err = command.RemoveIDFile(conf.Root)
	if err != nil {
		logger.Warningf("Failed to remove HostID file: %s", err)
	}
	return exitStatusOK
}
Ejemplo n.º 7
0
func main() {
	input := (&prompter.Prompter{
		Choices:    []string{"aa", "bb", "cc"},
		Default:    "aa",
		Message:    "plaase select",
		IgnoreCase: true,
	}).Prompt()
	fmt.Println("your input is " + input)

	input = (&prompter.Prompter{
		Message: "enter password",
		Regexp:  regexp.MustCompile(`.{8,}`),
		NoEcho:  true,
	}).Prompt()
	fmt.Println("your password is " + input)

	if prompter.YN("do you like sushi?", true) {
		fmt.Println("Nice!")
	} else {
		fmt.Println("It's Okay.")
	}

	if prompter.YesNo("do you like beer?", false) {
		fmt.Println("Nice!")
	} else {
		fmt.Println("It's Okay.")
	}

	passwd := prompter.Password("enter your password")
	fmt.Println("I got your password :P " + passwd)

	lang := prompter.Choose("Whitch language do you like the most?", []string{"Perl", "Golang", "Scala", "Ruby"}, "Perl")
	if lang == "Perl" {
		fmt.Println("So Nice!")
	} else {
		fmt.Println("I like also " + lang + " too.")
	}
}
Ejemplo n.º 8
0
func doDoctor(c *cli.Context) error {
	fixupIssues := c.Bool("fixup")
	ghqPath := verifyGhqPath()
	reposChannel := searchForRepos(ghqPath)

	// Listing repos
	for repo := range reposChannel {
		remoteOriginURL, _ := GitConfigGet(repo.Path, "remote.origin.url")
		trimmedRemote := compileTargetPathFromURL(remoteOriginURL)
		trimmedLocal := strings.TrimPrefix(repo.Path, ghqPath+"/")

		if remoteOriginURL == "" {
			fmt.Println("===>", trimmedLocal, "'remote.origin' doesn't exist")
			if fixupIssues {
				okToChange := prompter.YN("===> Fix remote.origin to"+trimmedLocal+"?", true)
				if okToChange {
					slp := strings.Split(trimmedLocal, "/")
					remotePathFromLocal := fmt.Sprintf("git@%s:%s/%s.git", slp[0], slp[1], slp[2])
					fmt.Println(remotePathFromLocal)
					err := GitRemoteAdd(repo.Path, "origin", remotePathFromLocal)
					if err != nil {
						fmt.Println("===> Fix failed")
						fmt.Println(err)
					} else {
						fmt.Println("===> Fixed")
						fmt.Println(remotePathFromLocal)
					}
				} else {
					fmt.Println()
				}
			}
		} else if trimmedRemote != trimmedLocal && !strings.Contains(trimmedLocal, "golang.org/x/") {
			fmt.Println("===>", trimmedLocal, "'remote.origin' has changed")
			fmt.Println("Remote:", trimmedRemote)
			if fixupIssues {
				fmt.Println("===> Fixup mode")
				fmt.Println("[1] " + trimmedLocal)
				fmt.Println("[2] " + trimmedRemote)
				choice := prompter.Choose("===> Choose the right location", []string{"1", "2"}, "1")
				if choice == "1" {
					// Change remote.origin
					slp := strings.Split(trimmedLocal, "/")
					remotePathFromLocal := fmt.Sprintf("git@%s:%s/%s.git", slp[0], slp[1], slp[2])
					err := GitRemoteSetURL(repo.Path, "origin", remotePathFromLocal)
					if err != nil {
						fmt.Println("===> Fix failed")
						fmt.Println(err)
						continue
					}
					fmt.Println("===> Fixed")
					fmt.Println(remotePathFromLocal)
				} else {
					// Move directory
					localPathFromRemote := filepath.Join(ghqPath, trimmedRemote)
					fmt.Println(localPathFromRemote)
					if _, err := os.Stat(localPathFromRemote); os.IsExist(err) {
						fmt.Println("===> Fix failed")
						fmt.Println(localPathFromRemote, "already exist")
						continue
					}

					if err := os.Rename(repo.Path, localPathFromRemote); err != nil {
						fmt.Println("===> Fix failed")
						fmt.Println(err)
						continue
					}
					fmt.Println("===> Fixed")
					fmt.Println(localPathFromRemote)
				}
			}
		}
	}
	return nil
}