⛩️ Powerful yet Minimal Nix Dependency Manager
flake flakes home-manager nixos go nix dependency dependencies
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

refactor(yae): move commands and library to internal package

Fuwn 3dd9f302 51e5de9a

+183 -132
+1 -1
.gitignore
··· 1 - yae 1 + /yae 2 2 .pre-commit-config.yaml 3 3 result 4 4 yae.json
+70
internal/commands/add.go
··· 1 + package commands 2 + 3 + import ( 4 + "fmt" 5 + "strings" 6 + 7 + "github.com/Fuwn/yae/internal/yae" 8 + "github.com/urfave/cli/v2" 9 + ) 10 + 11 + func Add(sources *yae.Sources) func(c *cli.Context) error { 12 + return func(c *cli.Context) error { 13 + if c.Args().Len() != 2 { 14 + return fmt.Errorf("invalid number of arguments") 15 + } 16 + 17 + if sources.Exists(c.Args().Get(0)) { 18 + return fmt.Errorf("source already exists") 19 + } 20 + 21 + source := yae.Source{ 22 + Unpack: c.Bool("unpack"), 23 + Type: c.String("type"), 24 + } 25 + version := c.String("version") 26 + 27 + if version != "" { 28 + source.URLTemplate = c.Args().Get(1) 29 + source.Version = c.String("version") 30 + 31 + if strings.Contains(source.URLTemplate, "{version}") { 32 + source.URL = strings.ReplaceAll(source.URLTemplate, "{version}", source.Version) 33 + } 34 + } else { 35 + source.URL = c.Args().Get(1) 36 + } 37 + 38 + if source.Type == "git" && c.String("tag-predicate") != "" { 39 + source.TagPredicate = c.String("tag-predicate") 40 + } 41 + 42 + if c.String("trim-tag-prefix") != "" { 43 + source.TrimTagPrefix = c.String("trim-tag-prefix") 44 + } 45 + 46 + if c.Bool("pin") { 47 + source.Pinned = true 48 + } 49 + 50 + if c.Bool("force") { 51 + if source.Pinned { 52 + return fmt.Errorf("cannot set a source to be statically forced and pinned at the same time") 53 + } 54 + 55 + source.Force = true 56 + } 57 + 58 + if sha256, err := yae.FetchSHA256(source.URL, c.Bool("unpack")); err != nil { 59 + return err 60 + } else { 61 + source.SHA256 = sha256 62 + } 63 + 64 + if err := sources.Add(c.Args().Get(0), source); err != nil { 65 + return err 66 + } 67 + 68 + return sources.Save(c.String("sources")) 69 + } 70 + }
+24
internal/commands/drop.go
··· 1 + package commands 2 + 3 + import ( 4 + "fmt" 5 + 6 + "github.com/Fuwn/yae/internal/yae" 7 + "github.com/urfave/cli/v2" 8 + ) 9 + 10 + func Drop(sources *yae.Sources) func(c *cli.Context) error { 11 + return func(c *cli.Context) error { 12 + if c.Args().Len() == 0 { 13 + return fmt.Errorf("invalid number of arguments") 14 + } 15 + 16 + if !sources.Exists(c.Args().Get(0)) { 17 + return fmt.Errorf("source does not exist") 18 + } 19 + 20 + sources.Drop(c.Args().Get(0)) 21 + 22 + return sources.Save(c.String("sources")) 23 + } 24 + }
+19
internal/commands/init.go
··· 1 + package commands 2 + 3 + import ( 4 + "fmt" 5 + "os" 6 + 7 + "github.com/Fuwn/yae/internal/yae" 8 + "github.com/urfave/cli/v2" 9 + ) 10 + 11 + func Init(sources *yae.Sources) func(c *cli.Context) error { 12 + return func(c *cli.Context) error { 13 + if _, err := os.Stat(c.String("sources")); err == nil { 14 + return fmt.Errorf("sources file already exists") 15 + } 16 + 17 + return sources.Save(c.String("sources")) 18 + } 19 + }
+51
internal/commands/update.go
··· 1 + package commands 2 + 3 + import ( 4 + "fmt" 5 + 6 + "github.com/Fuwn/yae/internal/yae" 7 + "github.com/urfave/cli/v2" 8 + ) 9 + 10 + func Update(sources *yae.Sources) func(c *cli.Context) error { 11 + return func(c *cli.Context) error { 12 + updates := []string{} 13 + force := c.Bool("force-hashed") 14 + forcePinned := c.Bool("force-pinned") 15 + 16 + if c.Args().Len() == 0 { 17 + for name, source := range *sources { 18 + if updated, err := source.Update(sources, name, force, forcePinned); err != nil { 19 + return err 20 + } else if updated { 21 + updates = append(updates, name) 22 + } 23 + } 24 + } else { 25 + name := c.Args().Get(0) 26 + source := (*sources)[name] 27 + 28 + if updated, err := source.Update(sources, name, force, forcePinned); err != nil { 29 + return err 30 + } else if updated { 31 + updates = append(updates, name) 32 + } 33 + } 34 + 35 + if len(updates) > 0 { 36 + if err := sources.Save(c.String("sources")); err != nil { 37 + return err 38 + } 39 + } 40 + 41 + if c.Bool("output-updated-list") { 42 + for _, update := range updates { 43 + fmt.Println(update) 44 + } 45 + } else if c.Bool("output-formatted-updated-list") { 46 + fmt.Println(yae.Lister(updates)) 47 + } 48 + 49 + return nil 50 + } 51 + }
+2 -2
source.go internal/yae/source.go
··· 1 - package main 1 + package yae 2 2 3 3 import ( 4 4 "fmt" ··· 71 71 72 72 log.Debugf("checking %s: sha256", name) 73 73 74 - sha256, err := fetchSHA256(source.URL, source.Unpack) 74 + sha256, err := FetchSHA256(source.URL, source.Unpack) 75 75 76 76 if err != nil { 77 77 return updated, err
+1 -1
sources.go internal/yae/sources.go
··· 1 - package main 1 + package yae 2 2 3 3 import ( 4 4 "encoding/json"
+3 -3
utilities.go internal/yae/utilities.go
··· 1 - package main 1 + package yae 2 2 3 3 import ( 4 4 "fmt" ··· 7 7 "strings" 8 8 ) 9 9 10 - func fetchSHA256(url string, unpack bool) (string, error) { 10 + func FetchSHA256(url string, unpack bool) (string, error) { 11 11 arguments := []string{"--type", "sha256", url} 12 12 13 13 if unpack { ··· 42 42 return string(out), err 43 43 } 44 44 45 - func lister(items []string) string { 45 + func Lister(items []string) string { 46 46 if len(items) == 0 { 47 47 return "" 48 48 } else if len(items) == 1 {
+12 -125
yae.go
··· 3 3 import ( 4 4 "fmt" 5 5 "os" 6 - "strings" 7 6 "time" 8 7 8 + "github.com/Fuwn/yae/internal/commands" 9 + "github.com/Fuwn/yae/internal/yae" 9 10 "github.com/charmbracelet/log" 10 11 "github.com/urfave/cli/v2" 11 12 ) 12 13 13 14 func main() { 14 - sources := Sources{} 15 + sources := yae.Sources{} 15 16 16 17 if err := (&cli.App{ 17 18 Name: "yae", ··· 65 66 Suggest: true, 66 67 Commands: []*cli.Command{ 67 68 { 68 - Name: "init", 69 - Usage: "Initialise a new Yae environment", 70 - Action: func(c *cli.Context) error { 71 - if _, err := os.Stat(c.String("sources")); err == nil { 72 - return fmt.Errorf("sources file already exists") 73 - } 74 - 75 - return sources.Save(c.String("sources")) 76 - }, 69 + Name: "init", 70 + Usage: "Initialise a new Yae environment", 71 + Action: commands.Init(&sources), 77 72 }, 78 73 { 79 74 Name: "add", ··· 119 114 Usage: "Always force update the source, regardless of unchanged remote tag", 120 115 }, 121 116 }, 122 - Action: func(c *cli.Context) error { 123 - if c.Args().Len() != 2 { 124 - return fmt.Errorf("invalid number of arguments") 125 - } 126 - 127 - if sources.Exists(c.Args().Get(0)) { 128 - return fmt.Errorf("source already exists") 129 - } 130 - 131 - source := Source{ 132 - Unpack: c.Bool("unpack"), 133 - Type: c.String("type"), 134 - } 135 - version := c.String("version") 136 - 137 - if version != "" { 138 - source.URLTemplate = c.Args().Get(1) 139 - source.Version = c.String("version") 140 - 141 - if strings.Contains(source.URLTemplate, "{version}") { 142 - source.URL = strings.ReplaceAll(source.URLTemplate, "{version}", source.Version) 143 - } 144 - } else { 145 - source.URL = c.Args().Get(1) 146 - } 147 - 148 - if source.Type == "git" && c.String("tag-predicate") != "" { 149 - source.TagPredicate = c.String("tag-predicate") 150 - } 151 - 152 - if c.String("trim-tag-prefix") != "" { 153 - source.TrimTagPrefix = c.String("trim-tag-prefix") 154 - } 155 - 156 - if c.Bool("pin") { 157 - source.Pinned = true 158 - } 159 - 160 - if c.Bool("force") { 161 - if source.Pinned { 162 - return fmt.Errorf("cannot set a source to be statically forced and pinned at the same time") 163 - } 164 - 165 - source.Force = true 166 - } 167 - 168 - if sha256, err := fetchSHA256(source.URL, c.Bool("unpack")); err != nil { 169 - return err 170 - } else { 171 - source.SHA256 = sha256 172 - } 173 - 174 - if err := sources.Add(c.Args().Get(0), source); err != nil { 175 - return err 176 - } 177 - 178 - return sources.Save(c.String("sources")) 179 - }, 117 + Action: commands.Add(&sources), 180 118 }, 181 119 { 182 - Name: "drop", 183 - Args: true, 184 - Usage: "Drop a source", 185 - Action: func(c *cli.Context) error { 186 - if c.Args().Len() == 0 { 187 - return fmt.Errorf("invalid number of arguments") 188 - } 189 - 190 - if !sources.Exists(c.Args().Get(0)) { 191 - return fmt.Errorf("source does not exist") 192 - } 193 - 194 - sources.Drop(c.Args().Get(0)) 195 - 196 - return sources.Save(c.String("sources")) 197 - }, 120 + Name: "drop", 121 + Args: true, 122 + Usage: "Drop a source", 123 + Action: commands.Drop(&sources), 198 124 }, 199 125 { 200 126 Name: "update", ··· 219 145 Usage: "Force updates for all sources, including pinned sources (can be used with --force-hashed)", 220 146 }, 221 147 }, 222 - Action: func(c *cli.Context) error { 223 - updates := []string{} 224 - force := c.Bool("force-hashed") 225 - forcePinned := c.Bool("force-pinned") 226 - 227 - if c.Args().Len() == 0 { 228 - for name, source := range sources { 229 - if updated, err := source.Update(&sources, name, force, forcePinned); err != nil { 230 - return err 231 - } else if updated { 232 - updates = append(updates, name) 233 - } 234 - } 235 - } else { 236 - name := c.Args().Get(0) 237 - source := sources[name] 238 - 239 - if updated, err := source.Update(&sources, name, force, forcePinned); err != nil { 240 - return err 241 - } else if updated { 242 - updates = append(updates, name) 243 - } 244 - } 245 - 246 - if len(updates) > 0 { 247 - if err := sources.Save(c.String("sources")); err != nil { 248 - return err 249 - } 250 - } 251 - 252 - if c.Bool("output-updated-list") { 253 - for _, update := range updates { 254 - fmt.Println(update) 255 - } 256 - } else if c.Bool("output-formatted-updated-list") { 257 - fmt.Println(lister(updates)) 258 - } 259 - 260 - return nil 261 - }, 148 + Action: commands.Update(&sources), 262 149 }, 263 150 }, 264 151 }).Run(os.Args); err != nil {