Beispiel #1
0
func runClusterRemove(args *docopt.Args) error {
	if err := readConfig(); err != nil {
		return err
	}

	name := args.String["<cluster-name>"]

	for i, s := range config.Servers {
		if s.Name != name {
			continue
		}
		config.Servers = append(config.Servers[:i], config.Servers[i+1:]...)

		f, err := os.Create(configPath())
		if err != nil {
			return err
		}
		defer f.Close()

		if len(config.Servers) != 0 {
			if err := toml.NewEncoder(f).Encode(config); err != nil {
				return err
			}
		}

		log.Printf("Server %q removed.", s.Name)
		return nil
	}
	return nil
}
Beispiel #2
0
func (c *Config) Marshal() []byte {
	var buf bytes.Buffer
	if err := toml.NewEncoder(&buf).Encode(c); err != nil {
		panic(err)
	}
	return buf.Bytes()
}
Beispiel #3
0
func runServerAdd(cmd *Command, args []string, client *controller.Client) error {
	if len(args) != 3 {
		cmd.printUsage(true)
	}
	if err := readConfig(); err != nil {
		return err
	}

	s := &ServerConfig{
		Name:    args[0],
		URL:     args[1],
		Key:     args[2],
		GitHost: serverGitHost,
		TLSPin:  serverTLSPin,
	}
	if serverGitHost == "" {
		u, err := url.Parse(s.URL)
		if err != nil {
			return err
		}
		if host, _, err := net.SplitHostPort(u.Host); err == nil {
			s.GitHost = host
		} else {
			s.GitHost = u.Host
		}
	}

	for _, existing := range config.Servers {
		if existing.Name == s.Name {
			return fmt.Errorf("Server %q already exists in ~/.flynnrc", s.Name)
		}
		if existing.URL == s.URL {
			return fmt.Errorf("A server with the URL %q already exists in ~/.flynnrc", s.URL)
		}
		if existing.GitHost == s.GitHost {
			return fmt.Errorf("A server with the git host %q already exists in ~/.flynnrc", s.GitHost)
		}
	}

	config.Servers = append(config.Servers, s)

	f, err := os.Create(configPath())
	if err != nil {
		return err
	}
	defer f.Close()

	if err := toml.NewEncoder(f).Encode(config); err != nil {
		return err
	}

	log.Printf("Server %q added.", s.Name)
	return nil
}
Beispiel #4
0
func runClusterAdd(args *docopt.Args) error {
	if err := readConfig(); err != nil {
		return err
	}

	serverGitHost := args.String["--git-host"]
	serverTLSPin := args.String["--tls-pin"]

	s := &ServerConfig{
		Name:    args.String["<cluster-name>"],
		URL:     args.String["<url>"],
		Key:     args.String["<key>"],
		GitHost: serverGitHost,
		TLSPin:  serverTLSPin,
	}
	if serverGitHost == "" {
		u, err := url.Parse(s.URL)
		if err != nil {
			return err
		}
		if host, _, err := net.SplitHostPort(u.Host); err == nil {
			s.GitHost = host
		} else {
			s.GitHost = u.Host
		}
	}

	for _, existing := range config.Servers {
		if existing.Name == s.Name {
			return fmt.Errorf("Server %q already exists in ~/.flynnrc", s.Name)
		}
		if existing.URL == s.URL {
			return fmt.Errorf("A server with the URL %q already exists in ~/.flynnrc", s.URL)
		}
		if existing.GitHost == s.GitHost {
			return fmt.Errorf("A server with the git host %q already exists in ~/.flynnrc", s.GitHost)
		}
	}

	config.Servers = append(config.Servers, s)

	f, err := os.Create(configPath())
	if err != nil {
		return err
	}
	defer f.Close()

	if err := toml.NewEncoder(f).Encode(config); err != nil {
		return err
	}

	log.Printf("Server %q added.", s.Name)
	return nil
}
Beispiel #5
0
func (c *Config) SaveTo(path string) error {
	f, err := os.Create(path)
	if err != nil {
		return err
	}
	defer f.Close()

	if len(c.Servers) != 0 {
		if err := toml.NewEncoder(f).Encode(c); err != nil {
			return err
		}
	}
	return nil
}
Beispiel #6
0
func (c *Config) SaveTo(path string) error {
	f, err := os.Create(path)
	if err != nil {
		return err
	}
	defer f.Close()

	if len(c.Clusters) != 0 {
		if err := toml.NewEncoder(f).Encode(c); err != nil {
			return err
		}
		f.Write([]byte("\n"))
	}
	return nil
}
Beispiel #7
0
func main() {
	if flag.NArg() != 0 {
		flag.Usage()
	}

	var tmp interface{}
	if err := json.NewDecoder(os.Stdin).Decode(&tmp); err != nil {
		log.Fatalf("Error decoding JSON: %s", err)
	}

	tomlData := translate(tmp)
	if err := toml.NewEncoder(os.Stdout).Encode(tomlData); err != nil {
		log.Fatalf("Error encoding TOML: %s", err)
	}
}