Esempio n. 1
0
// Lets the nickname for all configurations currently in the datastore
func listRecords(d *diskv.Diskv) error {
	var h Host
	keyChan, keyCount := d.Keys(nil), 0
	for key := range keyChan {
		val, err := d.Read(key)
		if err != nil {
			return err
		}

		err = json.Unmarshal(val, &h)
		if err != nil {
			return err
		}
		fmt.Printf("%s: %s\n", h.Nickname, val)
		keyCount++
	}
	fmt.Println("Total configuration(s) currently stored:", keyCount)
	return nil
}
Esempio n. 2
0
func writeFile(d *diskv.Diskv) error {
	home, err := getHome()
	if err != nil {
		return errors.New("failed to get vaild home directory")
	}

	var h []Host
	keyChan := d.Keys(nil)
	for key := range keyChan {
		val, err := d.Read(key)
		if err != nil {
			return err
		}

		var t Host
		err = json.Unmarshal(val, &t)
		if err != nil {
			return err
		}

		t.Key = getSSHKeyPath(home, t.Key)
		h = append(h, t)
	}

	configFile := path.Join(home, ".ssh", "config")
	fo, err := os.Create(configFile)
	if err != nil {
		return errors.New("could not write SSH configuration file")
	}

	temp := template.Must(template.New("configuration").Parse(configuration))
	err = temp.Execute(fo, h)
	if err != nil {
		return err
	}

	err = fo.Close()
	if err != nil {
		return errors.New("could not close SSH configuration file")
	}

	return nil
}