mirror of
https://github.com/taigrr/mg.git
synced 2026-04-02 03:28:42 -07:00
add stringer for stats
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
@@ -26,11 +27,12 @@ var importCmd = &cobra.Command{
|
|||||||
log.Println(err)
|
log.Println(err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
err = conf.Merge(parsed)
|
stats, err := conf.Merge(parsed)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
fmt.Println(stats)
|
||||||
} else {
|
} else {
|
||||||
f, err := os.ReadFile(args[0])
|
f, err := os.ReadFile(args[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -42,11 +44,12 @@ var importCmd = &cobra.Command{
|
|||||||
log.Println(err)
|
log.Println(err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
err = conf.Merge(parsed)
|
stats, err := conf.Merge(parsed)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
fmt.Println(stats)
|
||||||
}
|
}
|
||||||
err := conf.Save()
|
err := conf.Save()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package parse
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -50,19 +51,38 @@ func (m *MGConfig) AddRepo(path, remote string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MGConfig) Merge(m2 MGConfig) error {
|
type Stats struct {
|
||||||
|
Duplicates int
|
||||||
|
NewPaths []string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s Stats) String() string {
|
||||||
|
str := ""
|
||||||
|
for _, v := range s.NewPaths {
|
||||||
|
str += "Added repo " + v + "\n"
|
||||||
|
}
|
||||||
|
str += "\nAdded " + fmt.Sprintf("%d", len(s.NewPaths)) + " new repos\n"
|
||||||
|
str += "Skipped " + fmt.Sprintf("%d", s.Duplicates) + " duplicate repos"
|
||||||
|
return str
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MGConfig) Merge(m2 MGConfig) (Stats, error) {
|
||||||
|
stats := Stats{}
|
||||||
for _, v := range m2.Repos {
|
for _, v := range m2.Repos {
|
||||||
err := m.AddRepo(v.Path, v.Remote)
|
err := m.AddRepo(v.Path, v.Remote)
|
||||||
switch err {
|
switch err {
|
||||||
case errAlreadyRegistered:
|
case errAlreadyRegistered:
|
||||||
|
stats.Duplicates++
|
||||||
continue
|
continue
|
||||||
case nil:
|
case nil:
|
||||||
|
stats.NewPaths = append(stats.NewPaths, v.Path)
|
||||||
continue
|
continue
|
||||||
default:
|
default:
|
||||||
return err
|
|
||||||
|
return stats, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return stats, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadMGConfig loads the mgconfig file from the XDG_CONFIG_HOME directory
|
// LoadMGConfig loads the mgconfig file from the XDG_CONFIG_HOME directory
|
||||||
|
|||||||
Reference in New Issue
Block a user