2018-11-19 21:36:21 +00:00
|
|
|
package remote
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"text/tabwriter"
|
|
|
|
|
|
|
|
"git.cryptic.systems/fh-trier/go-flucky/pkg/config"
|
|
|
|
"git.cryptic.systems/fh-trier/go-flucky/pkg/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Add(remote *types.Remote, configDir string) error {
|
|
|
|
|
|
|
|
configuration, err := config.Read(configDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// search after duplicate remote_names
|
|
|
|
for _, r := range configuration.Remotes {
|
|
|
|
if r.Name == remote.Name {
|
|
|
|
return fmt.Errorf("Remote-Name %v already exists", remote.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
configuration.Remotes = append(configuration.Remotes, remote)
|
|
|
|
|
|
|
|
if err := config.Write(configuration, configDir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-11-28 11:32:34 +00:00
|
|
|
// Enable a remote link
|
|
|
|
func Enable(remoteName string, configDir string) error {
|
|
|
|
|
|
|
|
cnf, err := config.Read(configDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// search after duplicate remote_names
|
|
|
|
var found bool
|
|
|
|
for _, r := range cnf.Remotes {
|
|
|
|
if r.Name == remoteName {
|
|
|
|
r.Enabled = true
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
return fmt.Errorf("Can not find remote %v", remoteName)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := config.Write(cnf, configDir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disable a remote link
|
|
|
|
func Disable(remoteName string, configDir string) error {
|
|
|
|
|
|
|
|
cnf, err := config.Read(configDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// search after duplicate remote_names
|
|
|
|
var found bool
|
|
|
|
for _, r := range cnf.Remotes {
|
|
|
|
if r.Name == remoteName {
|
|
|
|
r.Enabled = false
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
return fmt.Errorf("Can not find remote %v", remoteName)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := config.Write(cnf, configDir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-11-19 21:36:21 +00:00
|
|
|
func List(w io.Writer, configDir string) error {
|
|
|
|
|
|
|
|
configuration, err := config.Read(configDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tw := tabwriter.NewWriter(w, 0, 0, 5, ' ', 0)
|
|
|
|
|
2018-11-28 11:32:34 +00:00
|
|
|
fmt.Fprint(tw, "name\taddress\tenabled\tregistered\n")
|
2018-11-19 21:36:21 +00:00
|
|
|
|
|
|
|
for _, remote := range configuration.Remotes {
|
2018-11-28 11:32:34 +00:00
|
|
|
fmt.Fprintf(tw, "%v\t%v\t%v\t%v\n", remote.Name, remote.Address, remote.Enabled, remote.Registered)
|
2018-11-19 21:36:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tw.Flush()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Remove(name string, configDir string) error {
|
|
|
|
|
|
|
|
con, err := config.Read(configDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var j int
|
|
|
|
for _, remote := range con.Remotes {
|
|
|
|
if remote.Name == name {
|
|
|
|
con.Remotes = append(con.Remotes[:j], con.Remotes[j+1:]...)
|
|
|
|
|
|
|
|
if j > 0 {
|
|
|
|
j = j - 1
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
j++
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := config.Write(con, configDir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func RemoveAll(configDir string) error {
|
|
|
|
|
|
|
|
con, err := config.Read(configDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
con.Remotes = nil
|
|
|
|
|
|
|
|
if err := config.Write(con, configDir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|