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
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
@@ -26,11 +27,12 @@ var importCmd = &cobra.Command{
|
||||
log.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
err = conf.Merge(parsed)
|
||||
stats, err := conf.Merge(parsed)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Println(stats)
|
||||
} else {
|
||||
f, err := os.ReadFile(args[0])
|
||||
if err != nil {
|
||||
@@ -42,11 +44,12 @@ var importCmd = &cobra.Command{
|
||||
log.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
err = conf.Merge(parsed)
|
||||
stats, err := conf.Merge(parsed)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Println(stats)
|
||||
}
|
||||
err := conf.Save()
|
||||
if err != nil {
|
||||
|
||||
@@ -2,6 +2,7 @@ package parse
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -50,19 +51,38 @@ func (m *MGConfig) AddRepo(path, remote string) error {
|
||||
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 {
|
||||
err := m.AddRepo(v.Path, v.Remote)
|
||||
switch err {
|
||||
case errAlreadyRegistered:
|
||||
stats.Duplicates++
|
||||
continue
|
||||
case nil:
|
||||
stats.NewPaths = append(stats.NewPaths, v.Path)
|
||||
continue
|
||||
default:
|
||||
return err
|
||||
|
||||
return stats, err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
return stats, nil
|
||||
}
|
||||
|
||||
// LoadMGConfig loads the mgconfig file from the XDG_CONFIG_HOME directory
|
||||
|
||||
Reference in New Issue
Block a user