示例#1
0
文件: config.go 项目: kless/wizard
// PostCheck checks and sets to be run after of.get configuration.
func (c *Conf) PostCheck(interactive, addConfig bool) error {
	// Email
	if !interactive {
		_, err := valid.Email().
			SetScheme(valid.NewScheme().Required()).
			Check(c.Email)
		if err != nil {
			return err
		}
	}

	c.Email = fmt.Sprintf("%s <%s>",
		c.Author, strings.Replace(c.Email, "@", " AT ", -1))

	// Adds extra fields to pass to templates.
	if !addConfig {
		c.ProjectHeader = strings.Repeat(_HEADER_CHAR, len(c.Project))

		if c.License != "none" {
			c.FullLicense = ListLicense[ListLowerLicense[c.License]]
		}
	}

	return nil
}
示例#2
0
文件: gowizard.go 项目: kless/wizard
// interactive uses the interactive mode.
func interactive(c *wizard.Conf, addConfig bool) (err error) {
	var sFlags []string
	var msg string

	// == Sorted flags
	if addConfig {
		msg = "New configuration"
		sFlags = []string{"author", "email", "license", "vcs", "import", "org"}
	} else {
		msg = "New project"
		sFlags = []string{
			"name",
			"org",
			"author",
			"email",
			"license",
			"vcs",
			"import",
		}
	}

	q := question.New()
	defer func() {
		err2 := q.Restore()
		if err2 != nil && err == nil {
			err = err2
		}
	}()

	fmt.Printf("\n  = Gowizard :: %s\n\n", msg)

	for _, k := range sFlags {
		f := flag.Lookup(k)

		if strings.Contains(f.Usage, ";") {
			f.Usage = strings.SplitN(f.Usage, ";", 2)[0]
		}
		f.Usage = strings.ToUpper(string(f.Usage[0])) + f.Usage[1:]

		switch k {
		case "name":
			q.Prompt(f.Usage,
				valid.String().SetStringCheck(valid.S_Strict),
				valid.NewScheme().Required().SetDefault(c.Project),
			)
			c.Project, err = q.ReadString()

			if err = c.SetNames(); err != nil {
				return err
			}
		case "org":
			isOrg := true

			if c.Org == "" {
				q.Prompt("Is for an organization?",
					valid.Bool(),
					valid.NewScheme().SetDefault(false),
				)
				isOrg, err = q.ReadBool()
			}

			if isOrg {
				q.Prompt(f.Usage,
					valid.String(),
					valid.NewScheme().Required().SetDefault(c.Org),
				)
				c.Org, err = q.ReadString()
			}
		case "author":
			q.Prompt(f.Usage,
				valid.String().SetStringCheck(valid.S_Strict),
				valid.NewScheme().Required().SetDefault(c.Author),
			)
			c.Author, err = q.ReadString()
		case "email":
			q.Prompt(f.Usage,
				valid.Email(),
				valid.NewScheme().Required().SetDefault(c.Email),
			)
			c.Email, err = q.ReadString()
		case "license":
			q.Prompt(f.Usage,
				valid.String(),
				valid.NewScheme().SetDefault(wizard.ListLowerLicense[c.License]),
			)
			c.License, err = q.ChoiceString(wizard.ListLicenseSorted)
			// It is got in upper case
			c.License = strings.ToLower(c.License)
		case "vcs":
			q.Prompt(f.Usage,
				valid.String(),
				valid.NewScheme().SetDefault(c.VCS),
			)
			c.VCS, err = q.ChoiceString(wizard.ListVCSsorted)
		case "import":
			if addConfig {
				q.Prompt(f.Usage,
					valid.String(),
					valid.NewScheme().Required(),
				)
				c.ImportPaths, err = q.ReadStringSlice()

			} else if len(c.ImportPaths) == 0 {
				tmp := ""

				q.Prompt(f.Usage,
					valid.String(),
					nil,
				)
				tmp, err = q.ReadString()

				if tmp != "" {
					c.ImportPaths = make([]string, 1)
					c.ImportPaths[0] = tmp
				}

			} else {
				q.Prompt(f.Usage,
					valid.String(),
					valid.NewScheme().SetDefault(c.ImportPaths[0]),
				)
				c.ImportPaths[0], err = q.ChoiceString(c.ImportPaths)
			}
		}

		if err != nil {
			return err
		}
	}

	fmt.Println()
	return nil
}