Example #1
0
func PrepareConfigurationMigration(dir string) (func() bool, func() error) {
	environmentLocation := filepath.Join(dir, "config.yml")
	env, err := loadOrInitializeEnvironment(environmentLocation)
	if err != nil {
		themekit.NotifyError(err)
		return func() bool { return false }, func() error { return err }
	}

	confirmationFn := func() bool {
		before, _ := ioutil.ReadFile(environmentLocation)
		after := env.String()
		fmt.Println(themekit.YellowText("Compare changes to configuration:"))
		fmt.Println(themekit.YellowText("Before:\n"), themekit.GreenText(string(before)))
		fmt.Println(themekit.YellowText("After:\n"), themekit.RedText(after))
		reader := bufio.NewReader(os.Stdin)
		fmt.Println(themekit.YellowText("Does this look correct? (y/n)"))
		text, _ := reader.ReadString('\n')
		return strings.TrimSpace(text) == "y"
	}

	saveFn := func() error {
		return env.Save(environmentLocation)
	}
	return confirmationFn, saveFn
}
Example #2
0
func writeToDisk(asset themekit.Asset, eventLog chan themekit.ThemeEvent) {
	dir, err := os.Getwd()
	if err != nil {
		themekit.NotifyError(err)
		return
	}

	perms, err := os.Stat(dir)
	if err != nil {
		themekit.NotifyError(err)
		return
	}

	filename := fmt.Sprintf("%s/%s", dir, asset.Key)
	err = os.MkdirAll(filepath.Dir(filename), perms.Mode())
	if err != nil {
		themekit.NotifyError(err)
		return
	}

	file, err := os.Create(filename)
	if err != nil {
		themekit.NotifyError(err)
		return
	}
	defer file.Sync()
	defer file.Close()

	var data []byte
	switch {
	case len(asset.Value) > 0:
		data = []byte(asset.Value)
	case len(asset.Attachment) > 0:
		data, err = base64.StdEncoding.DecodeString(asset.Attachment)
		if err != nil {
			themekit.NotifyError(errors.New(fmt.Sprintf("Could not decode %s. error: %s", asset.Key, err)))
			return
		}
	}

	if len(data) > 0 {
		_, err = file.Write(data)
	}

	if err != nil {
		themekit.NotifyError(err)
	} else {
		event := basicEvent{
			Title:     "FS Event",
			EventType: "Write",
			Target:    filename,
			etype:     "fsevent",
			Formatter: func(b basicEvent) string {
				return themekit.GreenText(fmt.Sprintf("Successfully wrote %s to disk", b.Target))
			},
		}
		logEvent(event, eventLog)
	}
}
Example #3
0
func spawnWorker(workerName string, queue chan themekit.AssetEvent, client themekit.ThemeClient, eventLog chan themekit.ThemeEvent) {
	logEvent(workerSpawnEvent(workerName), eventLog)
	for {
		asset := <-queue
		if asset.Asset().IsValid() {
			workerEvent := basicEvent{
				Title:     "FS Event",
				EventType: asset.Type().String(),
				Target:    asset.Asset().Key,
				etype:     "fsevent",
				Formatter: func(b basicEvent) string {
					return fmt.Sprintf(
						"Received %s event on %s",
						themekit.GreenText(b.EventType),
						themekit.BlueText(b.Target),
					)
				},
			}
			logEvent(workerEvent, eventLog)
			logEvent(client.Perform(asset), eventLog)
		}
	}
}