Skip to content

Commit

Permalink
Add examples command
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrie30 committed Aug 20, 2023
1 parent 6a62834 commit 98cb571
Show file tree
Hide file tree
Showing 424 changed files with 79,112 additions and 39 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)

## [1.9.8] - unreleased
### Added
- Examples command; thanks @MaxG87
### Changed
### Deprecated
### Removed
Expand Down
13 changes: 10 additions & 3 deletions cmd/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,17 @@ import (
)

var cloneCmd = &cobra.Command{
Use: "clone",
Use: "clone [org/user]",
Short: "Clone user or org repos from GitHub, GitLab, Gitea or Bitbucket",
Long: `Clone user or org repos from GitHub, GitLab, Gitea or Bitbucket. See $HOME/.config/ghorg/conf.yaml for defaults, its likely you will need to update some of these values of use the flags to overwrite them. Values are set first by a default value, then based off what is set in $HOME/.config/ghorg/conf.yaml, finally the cli flags, which have the highest level of precedence.`,
Run: cloneFunc,
Long: `Clone user or org repos from GitHub, GitLab, Gitea or Bitbucket. See $HOME/.config/ghorg/conf.yaml for defaults, its likely you will need to update some of these values of use the flags to overwrite them. Values are set first by a default value, then based off what is set in $HOME/.config/ghorg/conf.yaml, finally the cli flags, which have the highest level of precedence.
For complete examples of how to clone repos from each SCM provider, run one of the following examples commands:
$ ghorg examples github
$ ghorg examples gitlab
$ ghorg examples bitbucket
$ ghorg examples gitea
`,
Run: cloneFunc,
}

func cloneFunc(cmd *cobra.Command, argz []string) {
Expand Down
35 changes: 35 additions & 0 deletions cmd/examples.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package cmd

import (
"fmt"
"io/ioutil"

gtm "github.com/MichaelMure/go-term-markdown"
"github.com/gabrie30/ghorg/colorlog"
"github.com/spf13/cobra"
)

var examplesCmd = &cobra.Command{
Use: "examples",
Short: "Documentation and examples for each SCM provider",
Long: `Get documentation and examples for each SCM provider in the terminal`,
Run: examplesFunc,
}

func examplesFunc(cmd *cobra.Command, argz []string) {
filePath := fmt.Sprintf("examples/%s.md", argz[0])
input := getFileContents(filePath)
result := gtm.Render(string(input), 80, 6)
fmt.Println(string(result))
}

func getFileContents(filepath string) []byte {

contents, err := ioutil.ReadFile(filepath)
if err != nil {
colorlog.PrintErrorAndExit("Only supported SCM providers are available for examples, please use one of the following: github, gitlab, bitbucket, or gitea")
}

return contents

}
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func init() {
reCloneCmd.Flags().BoolVar(&ghorgReCloneQuiet, "quiet", false, "GHORG_RECLONE_QUIET - Quiet logging output")
reCloneCmd.Flags().BoolVar(&ghorgReCloneList, "list", false, "Prints reclone commands and optional descriptions to stdout then will exit 0. Does not obsfucate tokens, and is only available as a commandline argument")

rootCmd.AddCommand(lsCmd, versionCmd, cloneCmd, reCloneCmd)
rootCmd.AddCommand(lsCmd, versionCmd, cloneCmd, reCloneCmd, examplesCmd)
}

func Execute() {
Expand Down
69 changes: 34 additions & 35 deletions examples/gitlab.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,27 @@
To view all additional flags see the [sample-conf.yaml](https://github.com/gabrie30/ghorg/blob/master/sample-conf.yaml) or use `ghorg clone --help`

#### Things to know
## Things to know

1. There are differences in how ghorg works with GitLab on hosted instances vs GitLab cloud. Please make sure to follow the correct section below.

1. The `--preserve-dir` flag will mirror the nested directory structure of the groups/subgroups/projects locally to what is on GitLab. This prevents any name collisions with project names. If this flag is ommited all projects will be cloned into a single directory. If there are collisions with project names and `--preserve-dir` is not used the group/subgroup name will be prepended to those projects. An informational message will also be displayed during the clone to let you know if this happens.
1. The `--preserve-dir` flag will mirror the nested directory structure of the groups/subgroups/projects locally to what is on GitLab. This prevents any name collisions with project names. If this flag is omitted all projects will be cloned into a single directory. If there are collisions with project names and `--preserve-dir` is not used the group/subgroup name will be prepended to those projects. An informational message will also be displayed during the clone to let you know if this happens.

1. For all versions of GitLab you can clone groups or subgroups.

1. The `--output-dir` flag overrides the default name given to the folder ghorg creates to clone repos into. The default will be the instance name when cloning `all-groups` or `all-users` or the `group` name when cloning a specific group. The exception is when you are cloning a subgroup and preserving the directory structure, then it will preserve the parent groups of the subgroup.

1. If the group name you are cloning has spaces, substitute the spaces with `-` e.g.
1. If the group name you are cloning has spaces, substitute the spaces with "-" e.g.

> incorrect
```
ghorg clone "my group" --scm=gitlab
```sh
# incorrect
ghorg clone "my group" --scm=gitlab
ghorg clone my group --scm=gitlab
```

> correct
```
ghorg clone my-group --scm=gitlab
```sh
# correct
ghorg clone my-group --scm=gitlab
```

## Hosted GitLab Instances
Expand All @@ -38,13 +37,13 @@ To view all additional flags see the [sample-conf.yaml](https://github.com/gabri

1. Clone **all groups**, **preserving the directory structure** of subgroups

```
```sh
ghorg clone all-groups --base-url=https://<your.instance.gitlab.com> --scm=gitlab --token=XXXXXX --preserve-dir
```

This would produce a directory structure like

```
```sh
/GHORG_ABSOLUTE_PATH_TO_CLONE_TO
└── your.instance.gitlab
├── group1
Expand All @@ -59,13 +58,13 @@ To view all additional flags see the [sample-conf.yaml](https://github.com/gabri

1. Clone **all groups**, **WITHOUT preserving the directory structure** of subgroups

```
```sh
ghorg clone all-groups --base-url=https://<your.instance.gitlab.com> --scm=gitlab --token=XXXXXX
```

This would produce a directory structure like

```
```sh
/GHORG_ABSOLUTE_PATH_TO_CLONE_TO
└── your.instance.gitlab
├── project1
Expand All @@ -78,13 +77,13 @@ To view all additional flags see the [sample-conf.yaml](https://github.com/gabri

1. Clone **a specific group**, **preserving the directory structure** of subgroups

```
```sh
ghorg clone group3 --base-url=https://<your.instance.gitlab.com> --scm=gitlab --token=XXXXXX --preserve-dir
```

This would produce a directory structure like

```
```sh
/GHORG_ABSOLUTE_PATH_TO_CLONE_TO
└── group3
└── subgroup1
Expand All @@ -94,13 +93,13 @@ To view all additional flags see the [sample-conf.yaml](https://github.com/gabri

1. Clone **a specific group**, **WITHOUT preserving the directory structure** of subgroups

```
```sh
ghorg clone group3 --base-url=https://<your.instance.gitlab.com> --scm=gitlab --token=XXXXXX
```

This would produce a directory structure like

```
```sh
/GHORG_ABSOLUTE_PATH_TO_CLONE_TO
└── group3
├── project3
Expand All @@ -109,13 +108,13 @@ To view all additional flags see the [sample-conf.yaml](https://github.com/gabri

1. Clone **a specific subgroup**, **WITHOUT preserving the directory structure** of subgroups

```
```sh
ghorg clone group3/subgroup1 --base-url=https://<your.instance.gitlab.com> --scm=gitlab --token=XXXXXX
```

This would produce a directory structure like, where `projectX` is a project in a subgroup nested inside `subgroup1`

```
```sh
/GHORG_ABSOLUTE_PATH_TO_CLONE_TO
└── group3
└── subgroup1
Expand All @@ -126,13 +125,13 @@ To view all additional flags see the [sample-conf.yaml](https://github.com/gabri

1. Clone **a specific subgroup**, **preserving the directory structure** of subgroups

```
```sh
ghorg clone group3/subgroup1 --base-url=https://<your.instance.gitlab.com> --scm=gitlab --token=XXXXXX --preserve-dir
```

This would produce a directory structure like

```
```sh
/GHORG_ABSOLUTE_PATH_TO_CLONE_TO
└── group3
└── subgroup1
Expand All @@ -147,13 +146,13 @@ To view all additional flags see the [sample-conf.yaml](https://github.com/gabri

1. Clone a **user** on a **hosted gitlab** instance using a **token** for auth

```
```sh
ghorg clone <gitlab_username> --clone-type=user --base-url=https://<your.instance.gitlab.com> --scm=gitlab --token=bGVhdmUgYSBjb21tZW50IG9uIGlzc3VlIDY2
```

This would produce a directory structure like

```
```sh
/GHORG_ABSOLUTE_PATH_TO_CLONE_TO
└── gitlab_username
├── project3
Expand All @@ -168,13 +167,13 @@ To view all additional flags see the [sample-conf.yaml](https://github.com/gabri

1. Clone **all users**, **preserving the directory structure** of users

```
```sh
ghorg clone all-users --base-url=https://<your.instance.gitlab.com> --scm=gitlab --token=XXXXXX --preserve-dir
```

This would produce a directory structure like

```
```sh
/GHORG_ABSOLUTE_PATH_TO_CLONE_TO
└── your.instance.gitlab_users
├── user1
Expand All @@ -187,13 +186,13 @@ To view all additional flags see the [sample-conf.yaml](https://github.com/gabri
```
1. Clone **all users**, **WITHOUT preserving the directory structure** of users

```
```sh
ghorg clone all-users --base-url=https://<your.instance.gitlab.com> --scm=gitlab --token=XXXXXX
```

This would produce a directory structure like

```
```sh
/GHORG_ABSOLUTE_PATH_TO_CLONE_TO
└── your.instance.gitlab_users
├── project1
Expand All @@ -208,13 +207,13 @@ Examples below use the `gitlab-examples` GitLab cloud organization https://gitla

1. clone **all groups**, **preserving the directory structure** of subgroups

```
```sh
ghorg clone gitlab-examples --scm=gitlab --token=XXXXXX --preserve-dir
```

This would produce a directory structure like

```
```sh
/GHORG_ABSOLUTE_PATH_TO_CLONE_TO
└── gitlab-examples
├── aws-sam
Expand All @@ -230,13 +229,13 @@ Examples below use the `gitlab-examples` GitLab cloud organization https://gitla

1. clone only a **subgroup**, **preserving the directory structure** of subgroups

```
```sh
ghorg clone gitlab-examples/wayne-enterprises --scm=gitlab --token=XXXXXX --preserve-dir
```

This would produce a directory structure like

```
```sh
/GHORG_ABSOLUTE_PATH_TO_CLONE_TO
└── gitlab-examples
└── wayne-enterprises
Expand All @@ -257,13 +256,13 @@ Examples below use the `gitlab-examples` GitLab cloud organization https://gitla

1. clone only a **subgroup**, **WITHOUT preserving the directory structure** of subgroups

```
```sh
ghorg clone gitlab-examples/wayne-enterprises --scm=gitlab --token=XXXXXX
```

This would produce a directory structure like

```
```sh
/GHORG_ABSOLUTE_PATH_TO_CLONE_TO
└── wayne-enterprises
├── backend-controller
Expand Down
13 changes: 13 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.20

require (
code.gitea.io/sdk/gitea v0.15.1
github.com/MichaelMure/go-term-markdown v0.1.4
github.com/bradleyfalzon/ghinstallation/v2 v2.6.0
github.com/davecgh/go-spew v1.1.1
github.com/fatih/color v1.15.0
Expand All @@ -19,29 +20,41 @@ require (
)

require (
github.com/MichaelMure/go-term-text v0.3.1 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 // indirect
github.com/alecthomas/chroma v0.7.1 // indirect
github.com/cloudflare/circl v1.3.3 // indirect
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 // indirect
github.com/disintegration/imaging v1.6.2 // indirect
github.com/dlclark/regexp2 v1.1.6 // indirect
github.com/eliukblau/pixterm/pkg/ansimage v0.0.0-20191210081756-9fb6cf8c2f75 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/gomarkdown/markdown v0.0.0-20191123064959-2c17d62f5098 // indirect
github.com/google/go-github/v53 v53.2.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-retryablehttp v0.7.2 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/kyokomi/emoji/v2 v2.2.8 // indirect
github.com/lucasb-eyer/go-colorful v1.0.3 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mattn/go-runewidth v0.0.12 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/rivo/uniseg v0.1.0 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
golang.org/x/crypto v0.11.0 // indirect
golang.org/x/image v0.0.0-20220302094943-723b81ca9867 // indirect
golang.org/x/net v0.12.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
Expand Down
Loading

0 comments on commit 98cb571

Please sign in to comment.