Ejemplo n.º 1
0
func init() {
	jww.SetLogThreshold(jww.LevelTrace)
	jww.SetStdoutThreshold(jww.LevelInfo)

	log.Println("initing config ...")

	viper.SetConfigName("zookeeper-helper")
	viper.AddConfigPath("./")
	viper.AddConfigPath("$HOME/.omega/")
	viper.AddConfigPath("/etc/omega/")
	viper.SetConfigType("yaml")

	if err := viper.ReadInConfig(); err != nil {
		log.Panicln("can't read config file:", err)
	}

	initDefault()

	if err := viper.Unmarshal(&pairs); err != nil {
		log.Panicln("can't covert to config pairs: ", err)
	}

	if !pairs.Debugging {
		jww.SetLogThreshold(jww.LevelError)
		jww.SetStdoutThreshold(jww.LevelError)
	}

	log.Printf("initialized config pairs: %+v\n", pairs)
}
Ejemplo n.º 2
0
func init() {
	GrasshopperCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "verbose output")
	GrasshopperCmd.PersistentFlags().BoolVarP(&Quiet, "quiet", "q", false, "quiet output")
	GrasshopperCmd.PersistentFlags().BoolVarP(&Log, "log", "l", true, "write logging output to file")
	GrasshopperCmd.PersistentFlags().BoolVarP(&Experimental, "experimental", "x", true, "write experimental output to stdout")

	grasshopperCmdV = GrasshopperCmd

	viper.BindPFlag("verbose", GrasshopperCmd.PersistentFlags().Lookup("verbose"))
	viper.BindPFlag("quiet", GrasshopperCmd.PersistentFlags().Lookup("quiet"))
	viper.BindPFlag("log", GrasshopperCmd.PersistentFlags().Lookup("log"))
	viper.BindPFlag("experimental", GrasshopperCmd.PersistentFlags().Lookup("experimental"))

	if Log {
		jww.SetLogFile("grasshopper.log")
	}

	if Quiet {
		jww.SetStdoutThreshold(jww.LevelWarn)
	}

	if Verbose {
		jww.SetLogThreshold(jww.LevelTrace)
		jww.SetStdoutThreshold(jww.LevelTrace)
	}

}
Ejemplo n.º 3
0
Archivo: cfg.go Proyecto: nwlucas/cfg
func (c *Config) SetLogFile(s string) {
	if c.verbose {
		jww.SetLogThreshold(jww.LevelDebug)
		jww.SetStdoutThreshold(jww.LevelInfo)
	} else {
		jww.SetLogThreshold(jww.LevelWarn)
		jww.SetStdoutThreshold(jww.LevelError)
	}
	jww.SetLogFile(s)
}
Ejemplo n.º 4
0
func TestCheckExcuse(t *testing.T) {
	if testing.Verbose() {
		log.SetLogThreshold(log.LevelInfo)
		log.SetStdoutThreshold(log.LevelInfo)
	}

	createEmptyGitRepo(t)

	runCheckP(t, "foobar", true, "foo,5")
	// Increase on "barfoo" prefix is okay
	runCheckP(t, "barfoo", true, "foo,6")

	t.Logf("Running check command p: %s w: %t i: %s", "foobar", false, "foo,6")

	errCode := Check("foobar", 0, false, "csv", false, strings.NewReader("foo,6"))

	if errCode != 50 {
		t.Fatalf("Check command passed unexpectedly!")
	}

	writeExcuse(t, "foobar", "foo", "PROD's down right now, I'll clean foo up later")

	runCheckP(t, "foobar", true, "foo,6")

	t.Logf("Running check command p: %s w: %t i: %s", "barfoo", false, "foo,7")

	errCode = Check("barfoo", 0, false, "csv", false, strings.NewReader("foo,7"))

	if errCode != 50 {
		t.Fatalf("Check command passed unexpectedly!")
	}
}
Ejemplo n.º 5
0
func parser(cmd *cobra.Command, args []string) {
	if verbose {
		jww.SetStdoutThreshold(jww.LevelTrace)
	}
	opts := MQTT.NewClientOptions().AddBroker(viper.GetString("broker")).SetClientID("parser").SetCleanSession(true)

	client := MQTT.NewClient(opts)
	if token := client.Connect(); token.Wait() && token.Error() != nil {
		jww.FATAL.Println(token.Error())
		panic(token.Error())
	}
	defer client.Disconnect(0)

	fi, err := os.Stat(viper.GetString("port"))
	if err != nil {
		jww.FATAL.Println(err)
		panic(err)
	}
	if fi.Mode()&os.ModeType != 0 {
		serialLoop(client)
	} else {
		file, err := os.Open(viper.GetString("port"))
		if err != nil {
			jww.FATAL.Println(err)
			panic(err)
		}
		defer file.Close()
		loop(file, client)
	}
}
Ejemplo n.º 6
0
func TestDump(t *testing.T) {
	if testing.Verbose() {
		log.SetLogThreshold(log.LevelInfo)
		log.SetStdoutThreshold(log.LevelInfo)
	}

	repo := createEmptyGitRepo(t)

	runCheckP(t, "foo", true, "foo,5")
	runCommand(t, repo, exec.Command("git", "add", createFile(t, repo, "bar.txt").Name()))
	runCommand(t, repo, exec.Command("git", "commit", "-m", "Third Commit"))
	runCheckP(t, "foo", true, "foo,4")

	dump := bufio.NewScanner(bytes.NewReader(runDump(t, "foo").Bytes()))

	dump.Scan()

	checkString(t, "foo,4,4", dump.Text())

	dump.Scan()

	checkString(t, "foo,5,5", dump.Text())

	if len(runDump(t, "bar").Bytes()) > 0 {
		t.Fatalf("Should be no data under prefix bar")
	}
}
Ejemplo n.º 7
0
func TestCheckWithCheckstyleInput(t *testing.T) {
	if testing.Verbose() {
		log.SetLogThreshold(log.LevelInfo)
		log.SetStdoutThreshold(log.LevelInfo)
	}

	if checkStyleFileErr != nil {
		t.Fatalf("Failure opening test data", checkStyleFileErr)
	}

	createEmptyGitRepo(t)

	t.Logf("Running check command p: %s w: %t i: %s", "jshint", true, checkStyleFile)

	errCode := Check("jshint", 0, true, "checkstyle", false, checkStyleFile)

	if errCode != 0 {
		t.Fatalf("Check command failed! Error code: %d", errCode)
	}

	t.Logf("Running check command p: %s w: %t i: %s", "jshint", false, "errors,951")

	errCode = Check("jshint", 0, false, "csv", false, strings.NewReader("errors,951"))

	if errCode != 50 {
		t.Fatalf("Check command passed unexpectedly!")
	}
}
Ejemplo n.º 8
0
// initConfig reads in config file and ENV variables if set.
func initConfig() {
	if cfgFile != "" { // enable ability to specify config file via flag
		viper.SetConfigFile(cfgFile)
	}

	viper.SetConfigName(".gogetgithubstats") // name of config file (without extension)
	viper.AddConfigPath("$HOME")             // adding home directory as first search path
	viper.AutomaticEnv()                     // read in environment variables that match

	// This is the defaults
	viper.SetDefault("Verbose", true)

	// If a config file is found, read it in.
	err := viper.ReadInConfig()
	if err != nil {
		if _, ok := err.(viper.ConfigParseError); ok {
			jww.ERROR.Println(err)
		} else {
			jww.ERROR.Println("Unable to locate Config file.", err)
		}
	}

	if rootCmdV.PersistentFlags().Lookup("verbose").Changed {
		viper.Set("Verbose", Verbose)
	}

	if rootCmdV.PersistentFlags().Lookup("access-token").Changed {
		viper.Set("access-token", accessToken)
	}

	if viper.GetBool("verbose") {
		jww.SetStdoutThreshold(jww.LevelDebug)
	}
}
Ejemplo n.º 9
0
func TestZeroMissing(t *testing.T) {
	if testing.Verbose() {
		log.SetLogThreshold(log.LevelInfo)
		log.SetStdoutThreshold(log.LevelInfo)
	}

	createEmptyGitRepo(t)

	runCheck(t, true, "foo,5")

	t.Logf("Running check command w: %t i: %s", false, "")

	errCode := Check("", 0, true, "csv", false, strings.NewReader(""))

	if errCode != 50 {
		t.Fatalf("Check command passed unexpectedly!")
	}

	t.Logf("Running check command zero on missing w: %t i: %s", false, "")

	errCode = Check("", 0, true, "csv", false, strings.NewReader(""))

	if errCode != 0 {
		t.Fatalf("Check command failed unexpectedly!")
	}
}
Ejemplo n.º 10
0
func TestCheckSlackPercent(t *testing.T) {
	if testing.Verbose() {
		log.SetLogThreshold(log.LevelInfo)
		log.SetStdoutThreshold(log.LevelInfo)
	}

	createEmptyGitRepo(t)

	slack := 20.0
	usePercents := true

	runCheckPS(t, "pageweight", slack, usePercents, true, "gzippedjs,100")
	runCheckPS(t, "pageweight", slack, usePercents, false, "gzippedjs,101")

	t.Logf("Running check command p: %s w: %t i: %s", "pageweight", false, "gzippedjs,120")

	errCode := Check("pageweight", slack, usePercents, false, "csv", false, strings.NewReader("gzippedjs,120"))

	if errCode != 0 {
		t.Fatalf("Check command failed unexpectedly!")
	}

	t.Logf("Running check command p: %s w: %t i: %s", "pageweight", false, "gzippedjs,121")

	errCode = Check("pageweight", slack, usePercents, false, "csv", false, strings.NewReader("gzippedjs,121"))

	if errCode != 50 {
		t.Fatalf("Check command passed unexpectedly!")
	}
}
Ejemplo n.º 11
0
// InitializeConfig reads in config file and ENV variables if set.
func InitializeConfig(subCmdVs ...*cobra.Command) error {
	viper.SetConfigType("json")
	viper.SetConfigName("grasshopper") // name of config file (without extension)
	//	viper.AddConfigPath("/etc/grasshopper.d/")  // path to look for the config file
	//	viper.AddConfigPath("$HOME/.grasshopper.d") // call multiple times to add many search paths
	viper.AddConfigPath(".") // optionally look for config in the working directory

	// read config from storage
	err := viper.ReadInConfig()
	if err != nil {
		jww.WARN.Printf("Unable to read Config file. %#v I will fall back to my defaults...", err)
		err = nil // we just skip this error
	}

	// set some sane defaults
	viper.SetDefault("Verbose", false)
	viper.SetDefault("Quiet", false)
	viper.SetDefault("Log", true)
	viper.SetDefault("Experimental", true)

	if grasshopperCmdV.PersistentFlags().Lookup("verbose").Changed {
		viper.Set("Verbose", Verbose)
	}

	if viper.GetBool("verbose") {
		jww.SetStdoutThreshold(jww.LevelTrace)
		jww.SetLogThreshold(jww.LevelTrace)
	}

	return err
}
Ejemplo n.º 12
0
// InitializeConfig initializes a config file with sensible default configuration flags.
func InitializeConfig() {
	if Quiet {
		jww.SetStdoutThreshold(jww.LevelWarn)
	}

	viper.SetConfigName("grasshopper")          // name of config file (without extension)
	viper.AddConfigPath("/etc/grasshopper.d/")  // path to look for the config file
	viper.AddConfigPath("$HOME/.grasshopper.d") // call multiple times to add many search paths
	viper.AddConfigPath(".")                    // optionally look for config in the working directory

	// read config from storage
	err := viper.ReadInConfig() // FIXME
	if err != nil {
		jww.WARN.Println("Unable to locate Config file. I will fall back to my defaults...")
	}

	// default settings
	viper.SetDefault("Verbose", false)
	viper.SetDefault("Quiet", false)
	viper.SetDefault("DryRun", false)
	viper.SetDefault("DoLog", true)
	viper.SetDefault("Experimental", true)

	// bind config to command flags
	if grasshopperCmdV.PersistentFlags().Lookup("verbose").Changed {
		viper.Set("Verbose", Verbose)
	}
	if grasshopperCmdV.PersistentFlags().Lookup("quiet").Changed {
		viper.Set("Quiet", Quiet)
	}
	if grasshopperCmdV.PersistentFlags().Lookup("log").Changed {
		viper.Set("DoLog", DoLog)
	}
	if grasshopperCmdV.PersistentFlags().Lookup("experimental").Changed {
		viper.Set("Experimental", Experimental)
	}

	if DoLog {
		jww.SetLogFile("grasshopper.log")
	}

	if Verbose {
		jww.SetLogThreshold(jww.LevelTrace)
		jww.SetStdoutThreshold(jww.LevelTrace)
	}
}
Ejemplo n.º 13
0
// initialize logging...
func initLogging(configuration *Configuration) {

	log.SetLogFile(configuration.LogFile)

	threshold := configuration.LogThreshold
	if threshold == "TRACE" {
		log.SetLogThreshold(log.LevelTrace)
		log.SetStdoutThreshold(log.LevelTrace)
	} else if threshold == "DEBUG" {
		log.SetLogThreshold(log.LevelDebug)
		log.SetStdoutThreshold(log.LevelDebug)
	} else if threshold == "INFO" {
		log.SetLogThreshold(log.LevelInfo)
		log.SetStdoutThreshold(log.LevelInfo)
	} else if threshold == "WARN" {
		log.SetLogThreshold(log.LevelWarn)
		log.SetStdoutThreshold(log.LevelWarn)
	} else if threshold == "ERROR" {
		log.SetLogThreshold(log.LevelError)
		log.SetStdoutThreshold(log.LevelError)
	} else if threshold == "CRITICAL" {
		log.SetLogThreshold(log.LevelCritical)
		log.SetStdoutThreshold(log.LevelCritical)
	} else if threshold == "FATAL" {
		log.SetLogThreshold(log.LevelFatal)
		log.SetStdoutThreshold(log.LevelFatal)
	}
}
Ejemplo n.º 14
0
//FetchFunction is the function that downloads all Nulecule container images
func FetchFunction(cmd *cobra.Command, args []string) {
	if Verbose {
		jww.SetLogThreshold(jww.LevelTrace)
		jww.SetStdoutThreshold(jww.LevelInfo)
	}

	if len(args) < 1 {
		cmd.Usage()
		jww.FATAL.Println("URL to be fetched is missing")
	}

	jww.INFO.Printf("fetching: %q", strings.Join(args, " "))
}
Ejemplo n.º 15
0
func printIndexList(cmd *cobra.Command, args []string) {
	InitializeConfig()

	if Verbose {
		jww.SetLogThreshold(jww.LevelTrace)
		jww.SetStdoutThreshold(jww.LevelInfo)
	}

	nuleculeLibraryIndexZip, err := getNuleculeLibraryIndexfromGithubAsZIP()
	if err != nil {
		jww.FATAL.Println(err)
		return
	}

	w := new(tabwriter.Writer)
	w.Init(os.Stdout, 26, 8, 2, '\t', 0)

	fmt.Fprintln(w, "This is the Nulecule Library Index")
	fmt.Fprintln(w, "Application Name\tAppID\tVersion")

	// Iterate through the files in the archive
	for _, item := range nuleculeLibraryIndexZip.File {
		if item.FileInfo().IsDir() {
			continue
		}

		// if the file is a Nulecule, getit!
		if item.FileInfo().Name() == "Nulecule" {
			jww.DEBUG.Printf("Found a Nulecule, size of it's description is %d\n", item.FileInfo().Size())

			rc, err := item.Open()
			if err != nil {
				jww.FATAL.Println(err)
				return
			}
			defer rc.Close()

			// get the Nulecules content
			nuci, parseError := nulecule.Parse(rc)

			if parseError != nil {
				jww.INFO.Println(parseError, " This may be due to unsupported (by Grasshopper) artifact inheritance.")
				continue
			}

			fmt.Fprintf(w, "%s\t%s\t%s\n", nuci.Metadata.Name, nuci.AppID, nuci.Metadata.AppVersion)
		}

		w.Flush()
	}
}
Ejemplo n.º 16
0
func aggregator(cmd *cobra.Command, args []string) {
	if verbose {
		jww.SetStdoutThreshold(jww.LevelTrace)
	}
	db, err := data.OpenDatabase()
	if err != nil {
		jww.FATAL.Println(err)
		panic(err)
	}

	dataChannel := make(chan data.SensorData)

	topic := "/gowx/sample"
	opts := MQTT.NewClientOptions().AddBroker(viper.GetString("broker")).SetClientID("aggregator").SetCleanSession(true)

	opts.OnConnect = func(c *MQTT.Client) {
		if token := c.Subscribe(topic, 0, func(client *MQTT.Client, msg MQTT.Message) {
			r := bytes.NewReader(msg.Payload())
			decoder := json.NewDecoder(r)
			var data data.SensorData
			err := decoder.Decode(&data)
			if err != nil {
				jww.ERROR.Println(err)
				return
			}
			dataChannel <- data
		}); token.Wait() && token.Error() != nil {
			jww.FATAL.Println(token.Error())
			panic(token.Error())
		}
	}

	client := MQTT.NewClient(opts)
	if token := client.Connect(); token.Wait() && token.Error() != nil {
		jww.FATAL.Println(token.Error())
		panic(token.Error())
	}
	defer client.Disconnect(0)

	ticker := time.NewTicker(time.Duration(viper.GetInt("interval")) * time.Second)

	thedata := make(map[mapKey][]float64)
	for {
		select {
		case <-ticker.C:
			sumData(&thedata, db)
		case d := <-dataChannel:
			addData(&thedata, d)
		}
	}
}
Ejemplo n.º 17
0
//Execute adds all child commands to the root command GrasshopperCmd and sets flags appropriately.
func Execute() {
	jww.SetStdoutThreshold(jww.LevelInfo)

	jww.INFO.Printf("The Grasshopper has started at %s\n", time.Now())

	GrasshopperCmd.SuggestionsMinimumDistance = 1

	//add child commands to the root command.
	GrasshopperCmd.AddCommand(versionCmd)

	GrasshopperCmd.AddCommand(bashAutogenerateCmd)

	// add nulecule and it's sub-commands
	NuleculeCmd.AddCommand(IndexCmd)
	NuleculeCmd.AddCommand(GuessCmd)
	GrasshopperCmd.AddCommand(NuleculeCmd)

	// GrasshopperCmd.AddCommand(FetchCmd)
	// GrasshopperCmd.AddCommand(InstallCmd)
	// GrasshopperCmd.AddCommand(RunCmd)
	// GrasshopperCmd.AddCommand(StopCmd)
	// GrasshopperCmd.AddCommand(UninstallCmd)
	// GrasshopperCmd.AddCommand(CleanCmd)

	/* FIxME this is nice, but we need a better one!
	manHeader := &cobra.GenManHeader{
		Title:   "grasshopper",
		Section: "1",
	}
	out := new(bytes.Buffer)
	GrasshopperCmd.GenMan(manHeader, out)
	fmt.Println(out.String())
	*/

	/* FIxME this is nice, but we need a better one!
	manHeader := &cobra.GenManHeader{
		Title:   "grasshopper",
		Section: "1",
	}
	out := new(bytes.Buffer)
	GrasshopperCmd.GenMan(manHeader, out)
	fmt.Println(out.String())
	*/

	if err := GrasshopperCmd.Execute(); err != nil {
		// the err is already logged by Cobra
		os.Exit(-1)
	}

}
Ejemplo n.º 18
0
//Execute adds all child commands to the root command GrasshopperCmd and sets flags appropriately.
func Execute() {
	if Verbose {
		jww.SetLogThreshold(jww.LevelTrace)
		jww.SetStdoutThreshold(jww.LevelInfo)
	}

	if DoLog {
		jww.SetLogFile("grasshopper.log")
	}

	jww.DEBUG.Println("Gentlemen, start your engines!!")
	GrasshopperCmd.SuggestionsMinimumDistance = 1

	//add child commands to the root command.
	GrasshopperCmd.AddCommand(versionCmd)

	// FIXME unsupported bashAutogenerateCmd
	// GrasshopperCmd.AddCommand(bashAutogenerateCmd)

	GrasshopperCmd.AddCommand(IndexCmd)

	GrasshopperCmd.AddCommand(FetchCmd)
	GrasshopperCmd.AddCommand(InstallCmd)
	GrasshopperCmd.AddCommand(RunCmd)
	GrasshopperCmd.AddCommand(StopCmd)
	GrasshopperCmd.AddCommand(UninstallCmd)
	GrasshopperCmd.AddCommand(CleanCmd)

	/* FIxME this is nice, but we need a better one!
	manHeader := &cobra.GenManHeader{
		Title:   "grasshopper",
		Section: "1",
	}
	out := new(bytes.Buffer)
	GrasshopperCmd.GenMan(manHeader, out)
	fmt.Println(out.String())
	*/

	if err := GrasshopperCmd.Execute(); err != nil {
		// the err is already logged by Cobra
		os.Exit(-1)
	}

}
Ejemplo n.º 19
0
func TestCheckExcuse(t *testing.T) {
	if testing.Verbose() {
		log.SetLogThreshold(log.LevelInfo)
		log.SetStdoutThreshold(log.LevelInfo)
	}

	repo := createEmptyGitRepo(t)

	runCheckP(t, "foobar", true, "foo,5")
	// Increase on "barfoo" prefix is okay
	runCheckP(t, "barfoo", true, "foo,6")

	t.Logf("Running check command p: %s w: %t i: %s", "foobar", false, "foo,6")

	errCode := Check("foobar", 0, false, false, "csv", false, strings.NewReader("foo,6"))

	if errCode != 50 {
		t.Fatalf("Check command passed unexpectedly!")
	}

	writeExcuse(t, "foobar", "foo", "PROD's down right now, I'll clean foo up later")

	runCheckP(t, "foobar", true, "foo,6")

	t.Logf("Running check command p: %s w: %t i: %s", "barfoo", false, "foo,7")

	errCode = Check("barfoo", 0, false, false, "csv", false, strings.NewReader("foo,7"))

	if errCode != 50 {
		t.Fatalf("Check command passed unexpectedly!")
	}

	runCommand(t, repo, exec.Command("git", "add", createFile(t, repo, "test2.txt").Name()))
	runCommand(t, repo, exec.Command("git", "commit", "-m", "Third Commit"))

	runCheckP(t, "foobar", true, "foo,6")

	runCommand(t, repo, exec.Command("git", "add", createFile(t, repo, "test3.txt").Name()))
	runCommand(t, repo, exec.Command("git", "commit", "-m", "Fourth Commit"))

	runCheckP(t, "foobar", true, "foo,6")
}
Ejemplo n.º 20
0
func TestCheckPrefix(t *testing.T) {
	if testing.Verbose() {
		log.SetLogThreshold(log.LevelInfo)
		log.SetStdoutThreshold(log.LevelInfo)
	}

	createEmptyGitRepo(t)

	runCheckP(t, "foobar", true, "foo,5")
	// Running a check against a different prefix should still work
	runCheckP(t, "barfoo", true, "foo,6")

	t.Logf("Running check command p: %s w: %t i: %s", "foobar", false, "foo,6")

	errCode := Check("foobar", 0, false, "csv", false, strings.NewReader("foo,6"))

	if errCode != 50 {
		t.Fatalf("Check command passed unexpectedly!")
	}
}
Ejemplo n.º 21
0
Archivo: hugo.go Proyecto: nitoyon/hugo
// InitializeConfig initializes a config file with sensible default configuration flags.
// A Hugo command that calls initCoreCommonFlags() can pass itself
// as an argument to have its command-line flags processed here.
func InitializeConfig(subCmdVs ...*cobra.Command) error {
	viper.SetConfigFile(cfgFile)
	// See https://github.com/spf13/viper/issues/73#issuecomment-126970794
	if source == "" {
		viper.AddConfigPath(".")
	} else {
		viper.AddConfigPath(source)
	}
	err := viper.ReadInConfig()
	if err != nil {
		if _, ok := err.(viper.ConfigParseError); ok {
			return newSystemError(err)
		} else {
			return newSystemErrorF("Unable to locate Config file. Perhaps you need to create a new site.\n       Run `hugo help new` for details. (%s)\n", err)
		}
	}

	viper.RegisterAlias("indexes", "taxonomies")

	LoadDefaultSettings()

	for _, cmdV := range append([]*cobra.Command{hugoCmdV}, subCmdVs...) {

		if flagChanged(cmdV.PersistentFlags(), "verbose") {
			viper.Set("Verbose", verbose)
		}
		if flagChanged(cmdV.PersistentFlags(), "logFile") {
			viper.Set("LogFile", logFile)
		}
		if flagChanged(cmdV.Flags(), "cleanDestinationDir") {
			viper.Set("cleanDestinationDir", cleanDestination)
		}
		if flagChanged(cmdV.Flags(), "buildDrafts") {
			viper.Set("BuildDrafts", draft)
		}
		if flagChanged(cmdV.Flags(), "buildFuture") {
			viper.Set("BuildFuture", future)
		}
		if flagChanged(cmdV.Flags(), "uglyURLs") {
			viper.Set("UglyURLs", uglyURLs)
		}
		if flagChanged(cmdV.Flags(), "canonifyURLs") {
			viper.Set("CanonifyURLs", canonifyURLs)
		}
		if flagChanged(cmdV.Flags(), "disableRSS") {
			viper.Set("DisableRSS", disableRSS)
		}
		if flagChanged(cmdV.Flags(), "disableSitemap") {
			viper.Set("DisableSitemap", disableSitemap)
		}
		if flagChanged(cmdV.Flags(), "disableRobotsTXT") {
			viper.Set("DisableRobotsTXT", disableRobotsTXT)
		}
		if flagChanged(cmdV.Flags(), "pluralizeListTitles") {
			viper.Set("PluralizeListTitles", pluralizeListTitles)
		}
		if flagChanged(cmdV.Flags(), "preserveTaxonomyNames") {
			viper.Set("PreserveTaxonomyNames", preserveTaxonomyNames)
		}
		if flagChanged(cmdV.Flags(), "ignoreCache") {
			viper.Set("IgnoreCache", ignoreCache)
		}
		if flagChanged(cmdV.Flags(), "forceSyncStatic") {
			viper.Set("ForceSyncStatic", forceSync)
		}
		if flagChanged(cmdV.Flags(), "noTimes") {
			viper.Set("NoTimes", noTimes)
		}

	}

	if baseURL != "" {
		if !strings.HasSuffix(baseURL, "/") {
			baseURL = baseURL + "/"
		}
		viper.Set("BaseURL", baseURL)
	}

	if !viper.GetBool("RelativeURLs") && viper.GetString("BaseURL") == "" {
		jww.ERROR.Println("No 'baseurl' set in configuration or as a flag. Features like page menus will not work without one.")
	}

	if theme != "" {
		viper.Set("theme", theme)
	}

	if destination != "" {
		viper.Set("PublishDir", destination)
	}

	if source != "" {
		dir, _ := filepath.Abs(source)
		viper.Set("WorkingDir", dir)
	} else {
		dir, _ := os.Getwd()
		viper.Set("WorkingDir", dir)
	}

	if contentDir != "" {
		viper.Set("ContentDir", contentDir)
	}

	if layoutDir != "" {
		viper.Set("LayoutDir", layoutDir)
	}

	if cacheDir != "" {
		if helpers.FilePathSeparator != cacheDir[len(cacheDir)-1:] {
			cacheDir = cacheDir + helpers.FilePathSeparator
		}
		isDir, err := helpers.DirExists(cacheDir, hugofs.SourceFs)
		utils.CheckErr(err)
		if isDir == false {
			mkdir(cacheDir)
		}
		viper.Set("CacheDir", cacheDir)
	} else {
		viper.Set("CacheDir", helpers.GetTempDir("hugo_cache", hugofs.SourceFs))
	}

	if verboseLog || logging || (viper.IsSet("LogFile") && viper.GetString("LogFile") != "") {
		if viper.IsSet("LogFile") && viper.GetString("LogFile") != "" {
			jww.SetLogFile(viper.GetString("LogFile"))
		} else {
			jww.UseTempLogFile("hugo")
		}
	} else {
		jww.DiscardLogging()
	}

	if viper.GetBool("verbose") {
		jww.SetStdoutThreshold(jww.LevelInfo)
	}

	if verboseLog {
		jww.SetLogThreshold(jww.LevelInfo)
	}

	jww.INFO.Println("Using config file:", viper.ConfigFileUsed())

	themeDir := helpers.GetThemeDir()
	if themeDir != "" {
		if _, err := os.Stat(themeDir); os.IsNotExist(err) {
			return newSystemError("Unable to find theme Directory:", themeDir)
		}
	}

	themeVersionMismatch, minVersion := isThemeVsHugoVersionMismatch()

	if themeVersionMismatch {
		jww.ERROR.Printf("Current theme does not support Hugo version %s. Minimum version required is %s\n",
			helpers.HugoReleaseVersion(), minVersion)
	}

	return nil
}
Ejemplo n.º 22
0
// InitializeConfig initializes a config file with sensible default configuration flags.
func InitializeConfig() {
	viper.SetConfigFile(CfgFile)
	viper.AddConfigPath(Source)
	err := viper.ReadInConfig()
	if err != nil {
		jww.ERROR.Println("Unable to locate Config file. Perhaps you need to create a new site. Run `hugo help new` for details")
	}

	viper.RegisterAlias("indexes", "taxonomies")

	LoadDefaultSettings()

	if hugoCmdV.PersistentFlags().Lookup("buildDrafts").Changed {
		viper.Set("BuildDrafts", Draft)
	}

	if hugoCmdV.PersistentFlags().Lookup("buildFuture").Changed {
		viper.Set("BuildFuture", Future)
	}

	if hugoCmdV.PersistentFlags().Lookup("uglyUrls").Changed {
		viper.Set("UglyURLs", UglyURLs)
	}

	if hugoCmdV.PersistentFlags().Lookup("disableRSS").Changed {
		viper.Set("DisableRSS", DisableRSS)
	}

	if hugoCmdV.PersistentFlags().Lookup("disableSitemap").Changed {
		viper.Set("DisableSitemap", DisableSitemap)
	}

	if hugoCmdV.PersistentFlags().Lookup("verbose").Changed {
		viper.Set("Verbose", Verbose)
	}

	if hugoCmdV.PersistentFlags().Lookup("pluralizeListTitles").Changed {
		viper.Set("PluralizeListTitles", PluralizeListTitles)
	}

	if hugoCmdV.PersistentFlags().Lookup("preserveTaxonomyNames").Changed {
		viper.Set("PreserveTaxonomyNames", PreserveTaxonomyNames)
	}

	if hugoCmdV.PersistentFlags().Lookup("editor").Changed {
		viper.Set("NewContentEditor", Editor)
	}

	if hugoCmdV.PersistentFlags().Lookup("logFile").Changed {
		viper.Set("LogFile", LogFile)
	}
	if BaseURL != "" {
		if !strings.HasSuffix(BaseURL, "/") {
			BaseURL = BaseURL + "/"
		}
		viper.Set("BaseURL", BaseURL)
	}

	if !viper.GetBool("RelativeURLs") && viper.GetString("BaseURL") == "" {
		jww.ERROR.Println("No 'baseurl' set in configuration or as a flag. Features like page menus will not work without one.")
	}

	if Theme != "" {
		viper.Set("theme", Theme)
	}

	if Destination != "" {
		viper.Set("PublishDir", Destination)
	}

	if Source != "" {
		viper.Set("WorkingDir", Source)
	} else {
		dir, _ := os.Getwd()
		viper.Set("WorkingDir", dir)
	}

	if hugoCmdV.PersistentFlags().Lookup("ignoreCache").Changed {
		viper.Set("IgnoreCache", IgnoreCache)
	}

	if CacheDir != "" {
		if helpers.FilePathSeparator != CacheDir[len(CacheDir)-1:] {
			CacheDir = CacheDir + helpers.FilePathSeparator
		}
		isDir, err := helpers.DirExists(CacheDir, hugofs.SourceFs)
		utils.CheckErr(err)
		if isDir == false {
			mkdir(CacheDir)
		}
		viper.Set("CacheDir", CacheDir)
	} else {
		viper.Set("CacheDir", helpers.GetTempDir("hugo_cache", hugofs.SourceFs))
	}

	if VerboseLog || Logging || (viper.IsSet("LogFile") && viper.GetString("LogFile") != "") {
		if viper.IsSet("LogFile") && viper.GetString("LogFile") != "" {
			jww.SetLogFile(viper.GetString("LogFile"))
		} else {
			jww.UseTempLogFile("hugo")
		}
	} else {
		jww.DiscardLogging()
	}

	if viper.GetBool("verbose") {
		jww.SetStdoutThreshold(jww.LevelInfo)
	}

	if VerboseLog {
		jww.SetLogThreshold(jww.LevelInfo)
	}

	jww.INFO.Println("Using config file:", viper.ConfigFileUsed())

	themeDir := helpers.GetThemeDir()
	if themeDir != "" {
		if _, err := os.Stat(themeDir); os.IsNotExist(err) {
			jww.FATAL.Fatalln("Unable to find theme Directory:", themeDir)
		}
	}

	themeVersionMismatch, minVersion := helpers.IsThemeVsHugoVersionMismatch()

	if themeVersionMismatch {
		jww.ERROR.Printf("Current theme does not support Hugo version %s. Minimum version required is %s\n",
			helpers.HugoReleaseVersion(), minVersion)
	}
}
Ejemplo n.º 23
0
func main() {
	var write bool
	var verbose bool
	var prefix string
	var slack int
	var inputType string

	var versionCmd = &cobra.Command{
		Use:   "version",
		Short: "Print the version number",
		Long:  `All software has versions.`,
		Run: func(cmd *cobra.Command, args []string) {
			fmt.Printf("git-ratchet version: %s\n", GitTag)
		},
	}

	var checkCmd = &cobra.Command{
		Use:   "check",
		Short: "Checks the values passed in against the most recent stored values.",
		Long: `Checks the values passed in against the most recent stored values. 
The most recent stored values are found by walking up the commit graph and looking at the git-notes stored.`,
		Run: func(cmd *cobra.Command, args []string) {
			if verbose {
				log.SetLogThreshold(log.LevelInfo)
				log.SetStdoutThreshold(log.LevelInfo)
			}

			err := ratchet.Check(prefix, slack, write, inputType, os.Stdin)
			if err != 0 {
				os.Exit(err)
			}
		},
	}

	checkCmd.Flags().BoolVarP(&write, "write", "w", false, "write values if no increase is detected. only use on your CI server.")
	checkCmd.Flags().IntVarP(&slack, "slack", "s", 0, "slack value, increase within the range of the slack is acceptable.")
	checkCmd.Flags().StringVarP(&inputType, "inputType", "i", "csv", "input type. csv and checkstyle available.")

	var measure string
	var excuse string

	var excuseCmd = &cobra.Command{
		Use:   "excuse",
		Short: "Write an excuse for a measurement increase.",
		Long:  `Write an excuse for a measurement increase. This will allow the check command to pass.`,
		Run: func(cmd *cobra.Command, args []string) {
			if verbose {
				log.SetLogThreshold(log.LevelInfo)
				log.SetStdoutThreshold(log.LevelInfo)
			}

			os.Exit(ratchet.Excuse(prefix, measure, excuse))
		},
	}

	excuseCmd.Flags().StringVarP(&measure, "name", "n", "", "names of the measures to excuse, comma separated list.")
	excuseCmd.Flags().StringVarP(&excuse, "excuse", "e", "", "excuse for the measure rising.")

	var dumpCmd = &cobra.Command{
		Use:   "dump",
		Short: "Dump a CSV file containing the measurement data over time.",
		Long:  `Dump a CSV file containing the measurement data over time.`,
		Run: func(cmd *cobra.Command, args []string) {
			if verbose {
				log.SetLogThreshold(log.LevelInfo)
				log.SetStdoutThreshold(log.LevelInfo)
			}

			err := ratchet.Dump(prefix, os.Stdout)

			if err != 0 {
				os.Exit(err)
			}
		},
	}

	var rootCmd = &cobra.Command{Use: "git-ratchet"}
	rootCmd.AddCommand(checkCmd, excuseCmd, dumpCmd, versionCmd)
	rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "increase logging verbosity.")
	rootCmd.PersistentFlags().StringVarP(&prefix, "prefix", "p", "master", "prefix the ratchet notes. useful for storing multiple sets of values in the same repo.")

	rootCmd.Execute()
}
Ejemplo n.º 24
0
func importFromJekyll(cmd *cobra.Command, args []string) {
	jww.SetLogThreshold(jww.LevelTrace)
	jww.SetStdoutThreshold(jww.LevelWarn)

	if len(args) < 2 {
		jww.ERROR.Println(`Import from Jekyll requires two paths, e.g. ` + "`hugo import jekyll jekyll_root_path target_path`.")
		return
	}

	jekyllRoot, err := filepath.Abs(filepath.Clean(args[0]))
	if err != nil {
		jww.ERROR.Println("Path error:", args[0])
		return
	}

	targetDir, err := filepath.Abs(filepath.Clean(args[1]))
	if err != nil {
		jww.ERROR.Println("Path error:", args[1])
		return
	}

	createSiteFromJekyll(jekyllRoot, targetDir)

	jww.INFO.Println("Import Jekyll from:", jekyllRoot, "to:", targetDir)
	fmt.Println("Importing...")

	fileCount := 0
	callback := func(path string, fi os.FileInfo, err error) error {
		if err != nil {
			return err
		}

		if fi.IsDir() {
			return nil
		}

		relPath, err := filepath.Rel(jekyllRoot, path)
		if err != nil {
			jww.ERROR.Println("Get rel path error:", path)
			return err
		}

		relPath = filepath.ToSlash(relPath)
		var draft bool = false

		switch {
		case strings.HasPrefix(relPath, "_posts/"):
			relPath = "content/post" + relPath[len("_posts"):]
		case strings.HasPrefix(relPath, "_drafts/"):
			relPath = "content/draft" + relPath[len("_drafts"):]
			draft = true
		default:
			return nil
		}

		fileCount++
		return convertJekyllPost(path, relPath, targetDir, draft)
	}

	err = filepath.Walk(jekyllRoot, callback)

	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Println("Congratulations!", fileCount, "posts imported!")
		fmt.Println("Now, start Hugo by yourself: \n" +
			"$ git clone https://github.com/spf13/herring-cove.git " + args[1] + "/themes/herring-cove")
		fmt.Println("$ cd " + args[1] + "\n$ hugo server -w --theme=herring-cove")
	}
}
Ejemplo n.º 25
0
Archivo: hugo.go Proyecto: tubo028/hugo
// InitializeConfig initializes a config file with sensible default configuration flags.
func InitializeConfig(subCmdVs ...*cobra.Command) error {
	if err := hugolib.LoadGlobalConfig(source, cfgFile); err != nil {
		return err
	}

	for _, cmdV := range append([]*cobra.Command{hugoCmdV}, subCmdVs...) {

		if flagChanged(cmdV.PersistentFlags(), "verbose") {
			viper.Set("verbose", verbose)
		}
		if flagChanged(cmdV.PersistentFlags(), "logFile") {
			viper.Set("logFile", logFile)
		}
		if flagChanged(cmdV.Flags(), "cleanDestinationDir") {
			viper.Set("cleanDestinationDir", cleanDestination)
		}
		if flagChanged(cmdV.Flags(), "buildDrafts") {
			viper.Set("buildDrafts", draft)
		}
		if flagChanged(cmdV.Flags(), "buildFuture") {
			viper.Set("buildFuture", future)
		}
		if flagChanged(cmdV.Flags(), "buildExpired") {
			viper.Set("buildExpired", expired)
		}
		if flagChanged(cmdV.Flags(), "uglyURLs") {
			viper.Set("uglyURLs", uglyURLs)
		}
		if flagChanged(cmdV.Flags(), "canonifyURLs") {
			viper.Set("canonifyURLs", canonifyURLs)
		}
		if flagChanged(cmdV.Flags(), "disable404") {
			viper.Set("disable404", disable404)
		}
		if flagChanged(cmdV.Flags(), "disableRSS") {
			viper.Set("disableRSS", disableRSS)
		}
		if flagChanged(cmdV.Flags(), "disableSitemap") {
			viper.Set("disableSitemap", disableSitemap)
		}
		if flagChanged(cmdV.Flags(), "enableRobotsTXT") {
			viper.Set("enableRobotsTXT", enableRobotsTXT)
		}
		if flagChanged(cmdV.Flags(), "pluralizeListTitles") {
			viper.Set("pluralizeListTitles", pluralizeListTitles)
		}
		if flagChanged(cmdV.Flags(), "preserveTaxonomyNames") {
			viper.Set("preserveTaxonomyNames", preserveTaxonomyNames)
		}
		if flagChanged(cmdV.Flags(), "ignoreCache") {
			viper.Set("ignoreCache", ignoreCache)
		}
		if flagChanged(cmdV.Flags(), "forceSyncStatic") {
			viper.Set("forceSyncStatic", forceSync)
		}
		if flagChanged(cmdV.Flags(), "noTimes") {
			viper.Set("noTimes", noTimes)
		}

	}

	if baseURL != "" {
		if !strings.HasSuffix(baseURL, "/") {
			baseURL = baseURL + "/"
		}
		viper.Set("baseURL", baseURL)
	}

	if !viper.GetBool("relativeURLs") && viper.GetString("baseURL") == "" {
		jww.ERROR.Println("No 'baseurl' set in configuration or as a flag. Features like page menus will not work without one.")
	}

	if theme != "" {
		viper.Set("theme", theme)
	}

	if destination != "" {
		viper.Set("publishDir", destination)
	}

	var dir string
	if source != "" {
		dir, _ = filepath.Abs(source)
	} else {
		dir, _ = os.Getwd()
	}
	viper.Set("workingDir", dir)

	if contentDir != "" {
		viper.Set("contentDir", contentDir)
	}

	if layoutDir != "" {
		viper.Set("layoutDir", layoutDir)
	}

	if cacheDir != "" {
		viper.Set("cacheDir", cacheDir)
	}

	cacheDir = viper.GetString("cacheDir")
	if cacheDir != "" {
		if helpers.FilePathSeparator != cacheDir[len(cacheDir)-1:] {
			cacheDir = cacheDir + helpers.FilePathSeparator
		}
		isDir, err := helpers.DirExists(cacheDir, hugofs.Source())
		utils.CheckErr(err)
		if isDir == false {
			mkdir(cacheDir)
		}
		viper.Set("cacheDir", cacheDir)
	} else {
		viper.Set("cacheDir", helpers.GetTempDir("hugo_cache", hugofs.Source()))
	}

	if verboseLog || logging || (viper.IsSet("logFile") && viper.GetString("logFile") != "") {
		if viper.IsSet("logFile") && viper.GetString("logFile") != "" {
			jww.SetLogFile(viper.GetString("logFile"))
		} else {
			jww.UseTempLogFile("hugo")
		}
	} else {
		jww.DiscardLogging()
	}

	if quiet {
		jww.SetStdoutThreshold(jww.LevelError)
	} else if viper.GetBool("verbose") {
		jww.SetStdoutThreshold(jww.LevelInfo)
	}

	if verboseLog {
		jww.SetLogThreshold(jww.LevelInfo)
	}

	jww.INFO.Println("Using config file:", viper.ConfigFileUsed())

	// Init file systems. This may be changed at a later point.
	hugofs.InitDefaultFs()

	themeDir := helpers.GetThemeDir()
	if themeDir != "" {
		if _, err := hugofs.Source().Stat(themeDir); os.IsNotExist(err) {
			return newSystemError("Unable to find theme Directory:", themeDir)
		}
	}

	themeVersionMismatch, minVersion := isThemeVsHugoVersionMismatch()

	if themeVersionMismatch {
		jww.ERROR.Printf("Current theme does not support Hugo version %s. Minimum version required is %s\n",
			helpers.HugoReleaseVersion(), minVersion)
	}

	return nil

}
Ejemplo n.º 26
0
// InitializeConfig initializes a config file with sensible default configuration flags.
func InitializeConfig() {
	viper.SetConfigFile(CfgFile)
	viper.AddConfigPath(Source)
	err := viper.ReadInConfig()
	if err != nil {
		jww.ERROR.Println("Unable to locate Config file. Perhaps you need to create a new site. Run `hugo help new` for details")
	}

	viper.RegisterAlias("indexes", "taxonomies")

	viper.SetDefault("Watch", false)
	viper.SetDefault("MetaDataFormat", "toml")
	viper.SetDefault("DisableRSS", false)
	viper.SetDefault("DisableSitemap", false)
	viper.SetDefault("ContentDir", "content")
	viper.SetDefault("LayoutDir", "layouts")
	viper.SetDefault("StaticDir", "static")
	viper.SetDefault("ArchetypeDir", "archetypes")
	viper.SetDefault("PublishDir", "public")
	viper.SetDefault("DataDir", "data")
	viper.SetDefault("DefaultLayout", "post")
	viper.SetDefault("BuildDrafts", false)
	viper.SetDefault("BuildFuture", false)
	viper.SetDefault("UglyURLs", false)
	viper.SetDefault("Verbose", false)
	viper.SetDefault("IgnoreCache", false)
	viper.SetDefault("CanonifyURLs", false)
	viper.SetDefault("Taxonomies", map[string]string{"tag": "tags", "category": "categories"})
	viper.SetDefault("Permalinks", make(hugolib.PermalinkOverrides, 0))
	viper.SetDefault("Sitemap", hugolib.Sitemap{Priority: -1})
	viper.SetDefault("PygmentsStyle", "monokai")
	viper.SetDefault("DefaultExtension", "html")
	viper.SetDefault("PygmentsUseClasses", false)
	viper.SetDefault("DisableLiveReload", false)
	viper.SetDefault("PluralizeListTitles", true)
	viper.SetDefault("FootnoteAnchorPrefix", "")
	viper.SetDefault("FootnoteReturnLinkContents", "")
	viper.SetDefault("NewContentEditor", "")
	viper.SetDefault("Paginate", 10)
	viper.SetDefault("PaginatePath", "page")
	viper.SetDefault("Blackfriday", helpers.NewBlackfriday())

	if hugoCmdV.PersistentFlags().Lookup("buildDrafts").Changed {
		viper.Set("BuildDrafts", Draft)
	}

	if hugoCmdV.PersistentFlags().Lookup("buildFuture").Changed {
		viper.Set("BuildFuture", Future)
	}

	if hugoCmdV.PersistentFlags().Lookup("uglyUrls").Changed {
		viper.Set("UglyURLs", UglyURLs)
	}

	if hugoCmdV.PersistentFlags().Lookup("disableRSS").Changed {
		viper.Set("DisableRSS", DisableRSS)
	}

	if hugoCmdV.PersistentFlags().Lookup("disableSitemap").Changed {
		viper.Set("DisableSitemap", DisableSitemap)
	}

	if hugoCmdV.PersistentFlags().Lookup("verbose").Changed {
		viper.Set("Verbose", Verbose)
	}

	if hugoCmdV.PersistentFlags().Lookup("pluralizeListTitles").Changed {
		viper.Set("PluralizeListTitles", PluralizeListTitles)
	}

	if hugoCmdV.PersistentFlags().Lookup("editor").Changed {
		viper.Set("NewContentEditor", Editor)
	}

	if hugoCmdV.PersistentFlags().Lookup("logFile").Changed {
		viper.Set("LogFile", LogFile)
	}
	if BaseURL != "" {
		if !strings.HasSuffix(BaseURL, "/") {
			BaseURL = BaseURL + "/"
		}
		viper.Set("BaseURL", BaseURL)
	}

	if viper.GetString("BaseURL") == "" {
		jww.ERROR.Println("No 'baseurl' set in configuration or as a flag. Features like page menus will not work without one.")
	}

	if Theme != "" {
		viper.Set("theme", Theme)
	}

	if Destination != "" {
		viper.Set("PublishDir", Destination)
	}

	if Source != "" {
		viper.Set("WorkingDir", Source)
	} else {
		dir, _ := os.Getwd()
		viper.Set("WorkingDir", dir)
	}

	if hugoCmdV.PersistentFlags().Lookup("ignoreCache").Changed {
		viper.Set("IgnoreCache", IgnoreCache)
	}

	if CacheDir != "" {
		if helpers.FilePathSeparator != CacheDir[len(CacheDir)-1:] {
			CacheDir = CacheDir + helpers.FilePathSeparator
		}
		isDir, err := helpers.DirExists(CacheDir, hugofs.SourceFs)
		utils.CheckErr(err)
		if isDir == false {
			mkdir(CacheDir)
		}
		viper.Set("CacheDir", CacheDir)
	} else {
		viper.Set("CacheDir", helpers.GetTempDir("hugo_cache", hugofs.SourceFs))
	}

	if VerboseLog || Logging || (viper.IsSet("LogFile") && viper.GetString("LogFile") != "") {
		if viper.IsSet("LogFile") && viper.GetString("LogFile") != "" {
			jww.SetLogFile(viper.GetString("LogFile"))
		} else {
			jww.UseTempLogFile("hugo")
		}
	} else {
		jww.DiscardLogging()
	}

	if viper.GetBool("verbose") {
		jww.SetStdoutThreshold(jww.LevelInfo)
	}

	if VerboseLog {
		jww.SetLogThreshold(jww.LevelInfo)
	}

	jww.INFO.Println("Using config file:", viper.ConfigFileUsed())
}
Ejemplo n.º 27
0
func importFromJekyll(cmd *cobra.Command, args []string) error {
	jww.SetLogThreshold(jww.LevelTrace)
	jww.SetStdoutThreshold(jww.LevelWarn)

	if len(args) < 2 {
		return newUserError(`Import from Jekyll requires two paths, e.g. ` + "`hugo import jekyll jekyll_root_path target_path`.")
	}

	jekyllRoot, err := filepath.Abs(filepath.Clean(args[0]))
	if err != nil {
		return newUserError("Path error:", args[0])
	}

	targetDir, err := filepath.Abs(filepath.Clean(args[1]))
	if err != nil {
		return newUserError("Path error:", args[1])
	}

	jww.INFO.Println("Import Jekyll from:", jekyllRoot, "to:", targetDir)

	if strings.HasPrefix(filepath.Dir(targetDir), jekyllRoot) {
		return newUserError("Target path should not be inside the Jekyll root, aborting.")
	}

	forceImport, _ := cmd.Flags().GetBool("force")
	if err := createSiteFromJekyll(jekyllRoot, targetDir, forceImport); err != nil {
		return newUserError(err)
	}

	fmt.Println("Importing...")

	fileCount := 0
	callback := func(path string, fi os.FileInfo, err error) error {
		if err != nil {
			return err
		}

		if fi.IsDir() {
			return nil
		}

		relPath, err := filepath.Rel(jekyllRoot, path)
		if err != nil {
			return newUserError("Get rel path error:", path)
		}

		relPath = filepath.ToSlash(relPath)
		draft := false

		switch {
		case strings.HasPrefix(relPath, "_posts/"):
			relPath = "content/post" + relPath[len("_posts"):]
		case strings.HasPrefix(relPath, "_drafts/"):
			relPath = "content/draft" + relPath[len("_drafts"):]
			draft = true
		default:
			return nil
		}

		fileCount++
		return convertJekyllPost(path, relPath, targetDir, draft)
	}

	err = helpers.SymbolicWalk(hugofs.Os(), jekyllRoot, callback)

	if err != nil {
		return err
	}
	fmt.Println("Congratulations!", fileCount, "post(s) imported!")
	fmt.Println("Now, start Hugo by yourself:\n" +
		"$ git clone https://github.com/spf13/herring-cove.git " + args[1] + "/themes/herring-cove")
	fmt.Println("$ cd " + args[1] + "\n$ hugo server --theme=herring-cove")

	return nil
}
Ejemplo n.º 28
0
Archivo: hugo.go Proyecto: h0wn0w/hugo
func InitializeConfig() {
	viper.SetConfigName(CfgFile)
	viper.AddConfigPath(Source)
	viper.ReadInConfig()

	viper.SetDefault("ContentDir", "content")
	viper.SetDefault("LayoutDir", "layouts")
	viper.SetDefault("StaticDir", "static")
	viper.SetDefault("PublishDir", "public")
	viper.SetDefault("DefaultLayout", "post")
	viper.SetDefault("BuildDrafts", false)
	viper.SetDefault("UglyUrls", false)
	viper.SetDefault("Verbose", false)
	viper.SetDefault("CanonifyUrls", false)
	viper.SetDefault("Indexes", map[string]string{"tag": "tags", "category": "categories"})
	viper.SetDefault("Permalinks", make(hugolib.PermalinkOverrides, 0))

	if hugoCmdV.PersistentFlags().Lookup("build-drafts").Changed {
		viper.Set("BuildDrafts", Draft)
	}

	if hugoCmdV.PersistentFlags().Lookup("uglyurls").Changed {
		viper.Set("UglyUrls", UglyUrls)
	}

	if hugoCmdV.PersistentFlags().Lookup("verbose").Changed {
		viper.Set("Verbose", Verbose)
	}

	if hugoCmdV.PersistentFlags().Lookup("logfile").Changed {
		viper.Set("LogFile", LogFile)
	}

	if BaseUrl != "" {
		if !strings.HasSuffix(BaseUrl, "/") {
			BaseUrl = BaseUrl + "/"
		}
		viper.Set("BaseUrl", BaseUrl)
	}
	if Destination != "" {
		viper.Set("PublishDir", Destination)
	}

	if Source != "" {
		viper.Set("WorkingDir", Source)
	} else {
		dir, _ := helpers.FindCWD()
		viper.Set("WorkingDir", dir)
	}

	if VerboseLog || Logging || (viper.IsSet("LogFile") && viper.GetString("LogFile") != "") {
		if viper.IsSet("LogFile") && viper.GetString("LogFile") != "" {
			jww.SetLogFile(viper.GetString("LogFile"))
		} else {
			jww.UseTempLogFile("hugo")
		}
	} else {
		jww.DiscardLogging()
	}

	if viper.GetBool("verbose") {
		jww.SetStdoutThreshold(jww.LevelDebug)
	}

	if VerboseLog {
		jww.SetLogThreshold(jww.LevelDebug)
	}

	jww.INFO.Println("Using config file:", viper.ConfigFileUsed())
}
Ejemplo n.º 29
0
Archivo: hugo.go Proyecto: jaden/hugo
// InitializeConfig initializes a config file with sensible default configuration flags.
func InitializeConfig() {
	viper.SetConfigFile(CfgFile)
	viper.AddConfigPath(Source)
	err := viper.ReadInConfig()
	if err != nil {
		jww.ERROR.Println("Unable to locate Config file. Perhaps you need to create a new site. Run `hugo help new` for details")
	}

	viper.RegisterAlias("taxonomies", "indexes")

	viper.SetDefault("Watch", false)
	viper.SetDefault("MetaDataFormat", "toml")
	viper.SetDefault("DisableRSS", false)
	viper.SetDefault("DisableSitemap", false)
	viper.SetDefault("ContentDir", "content")
	viper.SetDefault("LayoutDir", "layouts")
	viper.SetDefault("StaticDir", "static")
	viper.SetDefault("ArchetypeDir", "archetypes")
	viper.SetDefault("PublishDir", "public")
	viper.SetDefault("DefaultLayout", "post")
	viper.SetDefault("BuildDrafts", false)
	viper.SetDefault("BuildFuture", false)
	viper.SetDefault("UglyUrls", false)
	viper.SetDefault("Verbose", false)
	viper.SetDefault("CanonifyUrls", false)
	viper.SetDefault("Indexes", map[string]string{"tag": "tags", "category": "categories"})
	viper.SetDefault("Permalinks", make(hugolib.PermalinkOverrides, 0))
	viper.SetDefault("Sitemap", hugolib.Sitemap{Priority: -1})
	viper.SetDefault("PygmentsStyle", "monokai")
	viper.SetDefault("DefaultExtension", "html")
	viper.SetDefault("PygmentsUseClasses", false)
	viper.SetDefault("DisableLiveReload", false)
	viper.SetDefault("PluralizeListTitles", true)
	viper.SetDefault("FootnoteAnchorPrefix", "")
	viper.SetDefault("FootnoteReturnLinkContents", "")
	viper.SetDefault("NewContentEditor", "")
	viper.SetDefault("Paginate", 10)
	viper.SetDefault("PaginatePath", "page")
	viper.SetDefault("Blackfriday", new(helpers.Blackfriday))

	if hugoCmdV.PersistentFlags().Lookup("buildDrafts").Changed {
		viper.Set("BuildDrafts", Draft)
	}

	if hugoCmdV.PersistentFlags().Lookup("buildFuture").Changed {
		viper.Set("BuildFuture", Future)
	}

	if hugoCmdV.PersistentFlags().Lookup("uglyUrls").Changed {
		viper.Set("UglyUrls", UglyUrls)
	}

	if hugoCmdV.PersistentFlags().Lookup("disableRSS").Changed {
		viper.Set("DisableRSS", DisableRSS)
	}

	if hugoCmdV.PersistentFlags().Lookup("disableSitemap").Changed {
		viper.Set("DisableSitemap", DisableSitemap)
	}

	if hugoCmdV.PersistentFlags().Lookup("verbose").Changed {
		viper.Set("Verbose", Verbose)
	}

	if hugoCmdV.PersistentFlags().Lookup("pluralizeListTitles").Changed {
		viper.Set("PluralizeListTitles", PluralizeListTitles)
	}

	if hugoCmdV.PersistentFlags().Lookup("editor").Changed {
		viper.Set("NewContentEditor", Editor)
	}

	if hugoCmdV.PersistentFlags().Lookup("logFile").Changed {
		viper.Set("LogFile", LogFile)
	}
	if BaseUrl != "" {
		if !strings.HasSuffix(BaseUrl, "/") {
			BaseUrl = BaseUrl + "/"
		}
		viper.Set("BaseUrl", BaseUrl)
	}

	if Theme != "" {
		viper.Set("theme", Theme)
	}

	if Destination != "" {
		viper.Set("PublishDir", Destination)
	}

	if Source != "" {
		viper.Set("WorkingDir", Source)
	} else {
		dir, _ := os.Getwd()
		viper.Set("WorkingDir", dir)
	}

	if VerboseLog || Logging || (viper.IsSet("LogFile") && viper.GetString("LogFile") != "") {
		if viper.IsSet("LogFile") && viper.GetString("LogFile") != "" {
			jww.SetLogFile(viper.GetString("LogFile"))
		} else {
			jww.UseTempLogFile("hugo")
		}
	} else {
		jww.DiscardLogging()
	}

	if viper.GetBool("verbose") {
		jww.SetStdoutThreshold(jww.LevelInfo)
	}

	if VerboseLog {
		jww.SetLogThreshold(jww.LevelInfo)
	}

	jww.INFO.Println("Using config file:", viper.ConfigFileUsed())
}
Ejemplo n.º 30
0
	Use:   "list",
	Short: "List all applications",
	Long:  `List all applications in the Nulecule Library Index.`,
	Run:   printIndexList,
}

var indexInfoCmd = &cobra.Command{
	Use:   "info APPNAME",
	Short: "show info of APPNAME",
	Long:  `Show detailed info of APPNAME in the Nulecule Library Index.`,
	Run: func(cmd *cobra.Command, args []string) {
		InitializeConfig()

		if Verbose {
			jww.SetLogThreshold(jww.LevelTrace)
			jww.SetStdoutThreshold(jww.LevelInfo)
		}

		if len(args) < 1 {
			cmd.Usage()
			jww.FATAL.Println("APPNAME is required")
		}

		nuleculeLibraryIndexZip, err := getNuleculeLibraryIndexfromGithubAsZIP()
		if err != nil {
			jww.FATAL.Println(err)
			return
		}

		w := new(tabwriter.Writer)
		w.Init(os.Stdout, 26, 8, 2, '\t', 0)