Ejemplo n.º 1
0
// Given a record allow settings to be updated.
func updateRecord(d *diskv.Diskv, name string) error {
	var h Host
	val, err := d.Read(md5sum(name))
	if err != nil {
		return fmt.Errorf("no configuration found for %s\n", name)
	}

	err = json.Unmarshal(val, &h)
	if err != nil {
		return fmt.Errorf("failed to parse the configuration: %s\n", err.Error())
	}

	err = h.updateConfig()
	if err != nil {
		return fmt.Errorf("failed to update the host information: %s", err.Error())
	}

	val, err = json.Marshal(h)
	if err != nil {
		return err
	}

	d.Write(md5sum(name), []byte(val))

	return nil
}
Ejemplo n.º 2
0
// Given a record remove it from from the datastore.
func removeRecord(d *diskv.Diskv, name string) error {
	err := d.Erase(md5sum(name))
	if err != nil {
		return fmt.Errorf("no configuration found for %s\n", name)
	}
	return nil
}
Ejemplo n.º 3
0
// Create the configuration record and store it in the datastore
func addRecord(d *diskv.Diskv, name, hostInfo string) error {
	h := Host{Nickname: name, KeepAlive: 30}
	var err error

	if hostInfo == "" {
		err = h.interactiveConfig()
		if err != nil {
			return err
		}
	} else {
		info := strings.Split(hostInfo, ":")
		err = h.config(info)
		if err != nil {
			return err
		}
	}

	val, err := json.Marshal(h)
	if err != nil {
		return err
	}

	d.Write(md5sum(name), []byte(val))
	return nil
}
Ejemplo n.º 4
0
// Fetch a record from the datastore and display the current configuration to the user
func getRecord(d *diskv.Diskv, name string) error {
	val, err := d.Read(md5sum(name))
	if err != nil {
		return fmt.Errorf("no configuration found for %s", name)
	}

	fmt.Println("Configuration for", name)
	fmt.Println(string(val))
	return nil
}
Ejemplo n.º 5
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
}
Ejemplo n.º 6
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
}