Skip to content

Commit

Permalink
simplified and streamlined repository package entirely
Browse files Browse the repository at this point in the history
  • Loading branch information
redradrat committed Oct 16, 2020
1 parent 0e8b47b commit 68292ae
Show file tree
Hide file tree
Showing 247 changed files with 24,635 additions and 9,182 deletions.
45 changes: 26 additions & 19 deletions cmd/addRepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ package cmd
import (
"errors"
"net/url"
"os"
"regexp"

kableerrors "github.com/redradrat/kable/pkg/errors"
"github.com/redradrat/kable/pkg/repositories"

"github.com/spf13/cobra"
)

var repoAuth bool

// addRepoCmd represents the add command
var addRepoCmd = &cobra.Command{
Use: "add [ID] [URL]",
Expand All @@ -52,23 +52,30 @@ var addRepoCmd = &cobra.Command{
PrintMsg("Fetching repository...")
name := args[0]
repoUrl := args[1]
err := repositories.AddRepository(name, repoUrl, "master")
if err != nil {
if err.Error() == "authentication required" {
user, pw, err := RunAuthDialog()
if err != nil {
PrintError("unable to display authentication dialog: %s", err)
}
err = repositories.AddAuthRepository(name, repoUrl, user, pw, "master")
if err != nil {
if !errors.Is(err, kableerrors.RepositoryAlreadyExistsError) {
PrintError("unable to add repository: %s", err)
} else {
PrintSuccess("Repository already configured!")
os.Exit(0)
}
}

var usr *string
if repoAuth {
user, pw, err := RunAuthDialog()
if err != nil {
PrintError("unable to display authentication dialog: %s", err)
}
if err := repositories.StoreRepoAuth(name, repositories.AuthPair{Username: user, Password: pw}); err != nil {
PrintError("unable to store authentication data: %s", err)
}
usr = &user
}

mod := repositories.AddRepository(repositories.Repository{
Name: name,
GitRepository: repositories.GitRepository{
URL: repoUrl,
GitRef: "refs/heads/master",
},
Username: usr,
})
err := repositories.UpdateRegistry(mod)
if err != nil {
PrintError("unable to update registry: %s", err)
}
PrintSuccess("Successfully added repository!")
},
Expand All @@ -85,5 +92,5 @@ func init() {

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// addCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
addRepoCmd.Flags().BoolVar(&repoAuth, "auth", false, "Whether auth is required to be stored for this repo.")
}
16 changes: 10 additions & 6 deletions cmd/listConcept.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ limitations under the License.
package cmd

import (
concepts2 "github.com/redradrat/kable/pkg/concepts"
"github.com/redradrat/kable/pkg/concepts"
"github.com/spf13/cobra"
)

Expand All @@ -25,16 +25,20 @@ var listConceptsCmd = &cobra.Command{
Use: "list",
Short: "List all available concepts",
Run: func(cmd *cobra.Command, args []string) {
concepts, err := concepts2.ListConcepts()
cis, err := concepts.ListConcepts()
if err != nil {
PrintError("unable to list concepts: %s", err)
}
var outList [][]string
for _, pair := range concepts {
for _, ci := range cis {
c, err := concepts.GetRepoConcept(ci)
if err != nil {
PrintError("error getting concept '%s': %s", ci.String(), err)
}
outList = append(outList, []string{
pair.Path,
pair.RepoId,
pair.Concept.Meta.Maintainer.String(),
ci.Concept(),
ci.Repo(),
c.Meta.Maintainer.String(),
})
}
PrintTable([]string{"ID", "Repository", "Maintainer"}, outList...)
Expand Down
17 changes: 5 additions & 12 deletions cmd/listRepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,25 @@ limitations under the License.
package cmd

import (
"strconv"

"github.com/redradrat/kable/pkg/repositories"
"github.com/spf13/cobra"
)

// listReposCmd represents the list command
var listReposCmd = &cobra.Command{
Use: "list",
Short: "Lists all concept repositories in the current config",
Short: "Lists all available concept repositories",
Run: func(cmd *cobra.Command, args []string) {
repoMap, err := repositories.ListRepositories()
repos, err := repositories.ListRepositories()
if err != nil {
PrintError("cannot list repositories: %s \n", err)
}

var repoSlices [][]string
for id, uri := range repoMap {
if repositories.IsInitialized(id) {
repoSlices = append(repoSlices, []string{id, uri, strconv.FormatBool(true)})
} else {
repoSlices = append(repoSlices, []string{id, uri, strconv.FormatBool(false)})
}
for _, repo := range repos {
repoSlices = append(repoSlices, []string{repo.Name, repo.URL, repo.GitRef})
}

PrintTable([]string{"ID", "URL", "Initialized"}, repoSlices...)
PrintTable([]string{"ID", "URL", "Ref"}, repoSlices...)
},
}

Expand Down
5 changes: 3 additions & 2 deletions cmd/removeRepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ var removeCmd = &cobra.Command{
},
Run: func(cmd *cobra.Command, args []string) {
PrintMsg("Removing repository...")
if err := repositories.RemoveRepository(args[0]); err != nil {
PrintError("unable to remove repository: %s \n", err)
mod := repositories.RemoveRepository(args[0])
if err := repositories.UpdateRegistry(mod); err != nil {
PrintError("unable to update repository registry: %s \n", err)
}
PrintSuccess("Successfully removed repository!")
}}
Expand Down
27 changes: 20 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ package cmd
import (
"fmt"
"os"
"path/filepath"

"github.com/redradrat/kable/pkg/config"
"github.com/spf13/viper"

"github.com/spf13/cobra"
)

var cfgFile string
var Config *config.Config

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{}
Expand All @@ -35,17 +35,30 @@ func init() {
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is $HOME/.config/kable/settings.json)")

//rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is $HOME/.config/kable/settings.json)")
//
// Cobra also supports local flags, which will only run
// when this action is called directly.
//rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

// initConfig reads in config file and ENV variables if set.
func initConfig() {
_, err := config.ReadConfig(cfgFile)
if err != nil {
PrintError("Cannot read config file: %s \n", err)
viper.SetConfigName("config") // name of config file (without extension)
viper.SetConfigType("json") // REQUIRED if the config file does not have the extension in the name
viper.AddConfigPath("/etc/kable/") // path to look for the config file in
viper.AddConfigPath("$HOME/.kable") // call multiple times to add many search paths

err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
hdir, err := os.UserHomeDir()
if err != nil {
panic("unable to get user homedir")
}
err = viper.WriteConfigAs(filepath.Join(hdir, "/.kable/config.json"))
if err != nil {
panic("unable to read and write config")
}
}

}
2 changes: 1 addition & 1 deletion cmd/tidyRepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var tidyCmd = &cobra.Command{
Short: "Clean up removed, cached repos",
Run: func(cmd *cobra.Command, args []string) {
PrintMsg("Tidying up cached repositories...")
err := repositories.TidyRepositories()
err := repositories.TidyCache()
if err != nil {
PrintError("unable to clean up repositories: %s", err)
}
Expand Down
13 changes: 7 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,27 @@ module github.com/redradrat/kable
go 1.14

require (
github.com/99designs/keyring v1.1.6
github.com/99designs/keyring v1.1.6 // indirect
github.com/AlecAivazis/survey/v2 v2.1.1
github.com/coreos/etcd v3.3.10+incompatible // indirect
github.com/fatih/color v1.9.0
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-git/go-git/v5 v5.1.0
github.com/google/go-jsonnet v0.16.1-0.20200908152747-b70cbd441a39 // indirect
github.com/google/uuid v1.1.1
github.com/google/uuid v1.1.1 // indirect
github.com/grafana/tanka v0.12.0
github.com/jsonnet-bundler/jsonnet-bundler v0.4.0
github.com/labstack/echo/v4 v4.1.16
github.com/manifoldco/promptui v0.7.0 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/olekukonko/tablewriter v0.0.4
github.com/otiai10/copy v1.2.0
github.com/otiai10/copy v1.2.0 // indirect
github.com/spf13/cobra v1.0.0
github.com/spf13/viper v1.4.0
github.com/stretchr/testify v1.6.1 // indirect
github.com/whilp/git-urls v1.0.0
github.com/zalando/go-keyring v0.1.0 // indirect
github.com/whilp/git-urls v1.0.0 // indirect
github.com/zalando/go-keyring v0.1.0
gopkg.in/yaml.v2 v2.3.0 // indirect
k8s.io/apimachinery v0.19.2 // indirect
)
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 h1:BHsljHzVlRcyQhjr
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/ghodss/yaml v0.0.0-20150909031657-73d445a93680/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
Expand Down
90 changes: 14 additions & 76 deletions pkg/concepts/concepts.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ import (
"github.com/redradrat/kable/pkg/repositories"

"github.com/redradrat/kable/pkg/errors"

"github.com/go-git/go-git/v5"
giturls "github.com/whilp/git-urls"
)

const (
Expand Down Expand Up @@ -183,21 +180,15 @@ func (iti InputTypeIdentifier) String() string {
}

func GetRepoConcept(cid ConceptIdentifier) (*Concept, error) {
path, err := GetRepoConceptPath(cid)
r, err := repositories.GetRepository(cid.Repo())
if err != nil {
return nil, err
}
return GetConcept(path)
}

func GetRepoConceptPath(cid ConceptIdentifier) (string, error) {
if !cid.IsValid() {
return "", errors.InvalidConceptIdentifierError
}
if !repositories.IsInitialized(cid.Repo()) {
return "", errors.RepositoryNotInitializedError
path, err := r.AbsolutePath()
if err != nil {
return nil, err
}
return filepath.Join(repositories.MustGetCacheInfo(cid.Repo()).AbsolutePath(), filepath.Join(cid.Concept())), nil
return GetConcept(filepath.Join(path, cid.Concept()))
}

func GetConcept(path string) (*Concept, error) {
Expand All @@ -218,44 +209,8 @@ type ConceptOrigin struct {
Ref string `json:"ref"`
}

func GetConceptOrigin(cid ConceptIdentifier) (*ConceptOrigin, error) {
if !cid.IsValid() {
return nil, errors.InvalidConceptIdentifierError
}
if !repositories.IsInitialized(cid.Repo()) {
return nil, errors.RepositoryNotInitializedError
}
repo, err := git.PlainOpen(repositories.MustGetCacheInfo(cid.Repo()).AbsolutePath())
if err != nil {
return nil, err
}

repoURL, err := repo.Remote(git.DefaultRemoteName)
if err != nil {
return nil, err
}

parsedURL, err := giturls.Parse(repoURL.Config().URLs[0])
if err != nil {
return nil, err
}

repoID := parsedURL.Host + parsedURL.Path

ref, err := repo.Head()
if err != nil {
return nil, err
}

origin := ConceptOrigin{
Repository: strings.TrimSuffix(repoID, ".git"),
Ref: ref.Hash().String(),
}
return &origin, nil
}

type ConceptRepoInfo struct {
Concept Concept
Concept string
Path string
RepoId string
}
Expand All @@ -276,39 +231,22 @@ func (mi MaintainerInfo) String() string {
return strings.Join(elements, " ")
}

func ListConceptsForRepo(repoid string) ([]ConceptRepoInfo, error) {
var concepts []ConceptRepoInfo
if !repositories.IsInitialized(repoid) {
return concepts, nil
}
ri, err := repositories.GetRepoIndex(repoid)
if err != nil {
return nil, err
}
for _, entry := range ri.ConceptEntries {
c, err := GetRepoConcept(NewConceptIdentifier(entry, repoid))
if err != nil {
return concepts, err
}
concepts = append(concepts, ConceptRepoInfo{RepoId: repoid, Path: entry, Concept: *c})
}
return concepts, nil
}

func ListConcepts() ([]ConceptRepoInfo, error) {
var repoList []ConceptRepoInfo
func ListConcepts() ([]ConceptIdentifier, error) {
var cis []ConceptIdentifier
repos, err := repositories.ListRepositories()
if err != nil {
return nil, err
}
for id, _ := range repos {
concepts, err := ListConceptsForRepo(id)
for _, repo := range repos {
idx, err := repo.RepoIndex()
if err != nil {
return nil, err
}
repoList = append(repoList, concepts...)
for _, path := range idx.ConceptEntries {
cis = append(cis, NewConceptIdentifier(path, repo.Name))
}
}
return repoList, nil
return cis, nil
}

func InitConcept(workdir, name string, conceptType ConceptType) error {
Expand Down
Loading

0 comments on commit 68292ae

Please sign in to comment.