From 040e67b857e74dcb48f0a89f18ceeacf6eb2778a Mon Sep 17 00:00:00 2001 From: 4lxprime <idalxprime@gmail.com> Date: Thu, 20 Jun 2024 15:11:54 +0200 Subject: [PATCH] [Arch+Docs]: move cli tool so we can exec the go command --- README.md | 12 ++++++--- cli.go | 64 +++++++++++++++++++++++++++++++++++++++++++++ cmd/gitdl/main.go | 66 ----------------------------------------------- gitdl.go | 2 +- 4 files changed, 73 insertions(+), 71 deletions(-) create mode 100644 cli.go delete mode 100644 cmd/gitdl/main.go diff --git a/README.md b/README.md index 467c560..5e2b647 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,10 @@ # gitdl - git downloader for specific folder +git downloader for specific folder - you can use it like this in your code: +## cli: +you can run the cli with `go run github.com/4lxprime/gitdl -h` from anywhere on your system as long as you have go installed. + +## code: ```golang if err := gitdl.DownloadGit( "torvalds/linux", // base the clone on https://github.com/torvalds/linux @@ -10,9 +13,10 @@ if err := gitdl.DownloadGit( gitdl.WithBranch("master"), // base the clone on the master branch (default main) gitdl.WithExclusions("configs/*", "async.c"), // exclude everything in configs/ and exclude async.c gitdl.WithReplace(gitdl.Map{"linux", "linuxisbetter"}) // everytime "linux" is mentionned, it will be remplaced by "linuxisbetter" + gitdl.WithLogs, // you want to enable log + gitdl.WithAuth("ghp_*") // use your github auth token so you can have likely unlimited requests + gitdl.WithoutChecksum // don't care about security (not recommended) ); err != nil { log.Fatal(err) } ``` - - or you can just use the cli tool with `go run cmd/gitdl/main.go -repo=torvalds/linux -folder=kernel -output=linux_kernel -logs` but there is no every options. diff --git a/cli.go b/cli.go new file mode 100644 index 0000000..59f2932 --- /dev/null +++ b/cli.go @@ -0,0 +1,64 @@ +package main + +import ( + "flag" + "fmt" + "log" + "strings" +) + +func main() { + repoFlag := flag.String("repo", "", "[required] the repo you want to download folder from") + folderFlag := flag.String("folder", "/", "[optional] folder you want to download from repo") + branchFlag := flag.String("branch", "main", "[optional] the branch you want to clone") + authFlag := flag.String("auth", "", "[optional] your github auth token") + outputFlag := flag.String("output", "", "[optional] local output folder") + logsFlag := flag.Bool("logs", true, "[optional] display downloading logs") + noChecksumFlag := flag.Bool("nochecksum", false, "[optional] disable downloading checksum") + + flag.Parse() + + if *repoFlag == "" { + log.Fatal(fmt.Errorf("you should specify the repo you want to clone (e.g. gitdl -folder=/ -output=gitdl 4lxprime/gitdl)")) + } + + if !strings.Contains(*repoFlag, "/") { + log.Fatal(fmt.Errorf("repo argument must contains / (e.g. 4lxprime/gitdl or github.com/4lxprime/gitdl)")) + } + + if *outputFlag == "" { + repoSplit := strings.Split(*repoFlag, "/") + repoName := repoSplit[len(repoSplit)-1] + + *outputFlag = repoName + } + + var options []Option + + if *logsFlag { + options = append(options, WithLogs) + } + + if *noChecksumFlag { + options = append(options, WithoutChecksum) + } + + if *authFlag != "" { + options = append(options, WithAuth(*authFlag)) + } + + options = append(options, WithBranch(*branchFlag)) // default set to main + + if err := DownloadGit( + *repoFlag, + *folderFlag, + *outputFlag, + options..., + ); err != nil { + log.Fatal(err) + } + + if *logsFlag { + log.Println("download completed successfully") + } +} diff --git a/cmd/gitdl/main.go b/cmd/gitdl/main.go deleted file mode 100644 index df596c6..0000000 --- a/cmd/gitdl/main.go +++ /dev/null @@ -1,66 +0,0 @@ -package main - -import ( - "flag" - "fmt" - "log" - "strings" - - "github.com/4lxprime/gitdl" -) - -func main() { - repoFlag := flag.String("repo", "", "the repo you want to download folder from") - folderFlag := flag.String("folder", "/", "folder you want to download from repo") - branchFlag := flag.String("branch", "main", "the branch you want to clone") - authFlag := flag.String("auth", "", "your github auth token") - outputFlag := flag.String("output", "", "local output folder") - logsFlag := flag.Bool("logs", true, "display downloading logs") - noChecksumFlag := flag.Bool("nochecksum", false, "disable downloading checksum") - - flag.Parse() - - if *repoFlag == "" { - log.Fatal(fmt.Errorf("you should specify the repo you want to clone (e.g. gitdl -folder=/ -output=gitdl 4lxprime/gitdl)")) - } - - if !strings.Contains(*repoFlag, "/") { - log.Fatal(fmt.Errorf("repo argument must contains / (e.g. 4lxprime/gitdl or github.com/4lxprime/gitdl)")) - } - - if *outputFlag == "" { - repoSplit := strings.Split(*repoFlag, "/") - repoName := repoSplit[len(repoSplit)-1] - - *outputFlag = repoName - } - - var options []gitdl.Option - - if *logsFlag { - options = append(options, gitdl.WithLogs) - } - - if *noChecksumFlag { - options = append(options, gitdl.WithoutChecksum) - } - - if *authFlag != "" { - options = append(options, gitdl.WithAuth(*authFlag)) - } - - options = append(options, gitdl.WithBranch(*branchFlag)) // default set to main - - if err := gitdl.DownloadGit( - *repoFlag, - *folderFlag, - *outputFlag, - options..., - ); err != nil { - log.Fatal(err) - } - - if *logsFlag { - log.Println("download completed successfully") - } -} diff --git a/gitdl.go b/gitdl.go index 32fdfa6..c95ba43 100644 --- a/gitdl.go +++ b/gitdl.go @@ -1,4 +1,4 @@ -package gitdl +package main import ( "bytes"