12 Commits

Author SHA1 Message Date
c476591e00 Merge branch 'release/0.0.2' 2020-05-23 12:03:29 +02:00
aee3dbad18 Update README 2020-05-23 12:03:17 +02:00
0fa845b3ff Add basic interactive overwrite strategy
Available options:

- Do not copy
- Overwrite dest
- Copy source as dest.dist
2020-05-23 11:57:49 +02:00
6aaa82efa4 Fix typo 2020-05-18 14:16:35 +02:00
e13d7f6fe4 Mise à jour de 'doc/scaffold_file_format.md' 2020-05-18 14:10:37 +02:00
ee328d8d46 Mise à jour de 'doc/scaffold_file_format.md' 2020-05-18 14:10:08 +02:00
b000ad4a20 Add scaffold.yml file format base documentation 2020-05-18 14:02:27 +02:00
37a29e4524 Merge tag '0.0.1' into develop
First release
2020-05-18 12:17:19 +02:00
fa1856ed8f Merge branch 'release/0.0.1' 2020-05-18 12:17:08 +02:00
4dcb662659 Remove pre-alpha mention 2020-05-18 12:16:57 +02:00
5aae615920 Add gitea-release task 2020-05-18 12:15:34 +02:00
928bc414e4 Mise à jour de 'README.md' 2020-04-07 15:36:42 +02:00
17 changed files with 405 additions and 27 deletions

3
.env.dist Normal file
View File

@ -0,0 +1,3 @@
GITEA_RELEASE_PROJECT=
GITEA_RELEASE_ORG=
GITEA_RELEASE_USERNAME=

3
.gitignore vendored
View File

@ -2,4 +2,5 @@
/bin
rice-box.go
/release
/vendor
/vendor
/.env

View File

@ -22,6 +22,9 @@ vendor:
release: clean vendor
./misc/script/release
gitea-release:
./misc/script/gitea-release
clean:
rm -rf ./bin ./release ./coverage

View File

@ -1,7 +1,5 @@
# Scaffold
> **/!\\** Statut: préalpha
Utilitaire de génération d'arborescence de fichiers à partir de projets "modèles" distants/locaux.
## Installation
@ -14,9 +12,8 @@ go install forge.cadoles.com/wpetit/scaffold/cmd/scaffold
### À partir des binaires
```
TODO
```
1. [Télécharger la dernière version du binaire correspondant à votre plateforme](https://forge.cadoles.com/wpetit/scaffold/releases) (dernière version stable: 0.0.2)
2. Extraire l'archive et place le binaire `bin/scaffold` dans votre `$PATH` (par exemple dans le répertoire `/usr/local/bin`)
## Usage
@ -35,15 +32,28 @@ GLOBAL OPTIONS:
--help, -h show help (default: false)
```
### `scaffold from [command options] <URL>`
### Commandes
#### `from`
```
NAME:
scaffold from - generate a new project from a given template url
USAGE:
scaffold from [command options] <URL>
OPTIONS:
--directory DIR, -d DIR Set destination to DIR (default: "./")
--manifest FILE, -m FILE The scaffold manifest FILE (default: "scaffold.yml")
--ignore value, -i value Ignore files matching pattern (default: ".git", ".git/**", "./.git/**/*")
--help, -h show help (default: false)
```
## Documentation
- [Format du fichier `scaffold.yml`](./doc/scaffold_file_format.md)
## Licence
[GPL-3.0](./LICENSE)

View File

@ -16,7 +16,7 @@ func main() {
}
if err := app.Run(os.Args); err != nil {
fmt.Printf("%+v", err)
fmt.Printf("%+v\n", err)
os.Exit(1)
}
}

View File

@ -0,0 +1,3 @@
# Créer un nouveau modèle de projet
Un modèle de projet `scaffold` est un simple répertoire (ou dépôt Git) contenant à sa racine un fichier `scaffold.yml`.

View File

View File

@ -0,0 +1,71 @@
## Format du fichier `scaffold.yml`
Le fichier `scaffold.yml` est au format [YAML](https://yaml.org/). C'est ce fichier qui permet de définir les données à injecter dans les modèles lors de la génération d'un nouveau projet avec la commande `scaffold from ...`.
**Schéma**
``` yaml
# Version du format de fichier. Actuellement, la seule valeur possible est 1.
version: 1
# Liste des variables à injecter dans les modèles de fichier
vars:
- <Var>
# - <Var>...
```
## Types
### `<Var>`
Définition d'une variable injectable dans les modèles de fichier.
**Schéma**
```yaml
# Type de la variable. Actuellement, la seul valeur possible est "string"
type: "string"
# Nom de la variable. C'est le nom qui permettra d'accéder à la valeur de la variable dans les modèles de fichiers.
name: <string>
# Description de la variable. C'est le texte qui sera affiché dans l'invite de commande au moment de la génération de l'arborescence de fichiers.
description: <string>
# Liste des contraintes appliquées à la valeur de la variable. La valeur est considérée comme invalide tant que l'ensemble des contraintes ne sont pas remplies.
constraints:
- <Constraint>
# - <Constraint>...
```
### `<Constraint>`
Définition d'une contrainte appliquée à la valeur d'une variable.
```yaml
# Règle de validation à appliquer à la valeur entrée par l'utilisateur.
rule: <Rule>
# Message à afficher à l'utilisateur si la règle de validation concorde avec la valeur entrée par celui ci.
message: <string>
```
**Exemple**
```yaml
# Règle validant que la valeur ne doit pas être vide.
rule: Input == ""
message: Cette valeur ne peut être vide.
```
### `<Rule>`
Règle de validation d'une contrainte appliquée à une variable.
Ce n'est ni plus ni moins qu'une condition booléenne écrite avec la syntaxe proposée par la librairie [github.com/antonmedv/expr](https://github.com/antonmedv/expr/blob/master/docs/Language-Definition.md).
Voici les variables globales disponibles aux règles:
|Variable|Description|
|--------|-----------|
|`Input` |Valeur entrée par l'utilisateur|

2
go.mod
View File

@ -7,10 +7,12 @@ require (
github.com/Masterminds/semver v1.5.0 // indirect
github.com/Masterminds/sprig v2.22.0+incompatible
github.com/antonmedv/expr v1.4.5
github.com/davecgh/go-spew v1.1.1
github.com/google/uuid v1.1.1 // indirect
github.com/huandu/xstrings v1.3.0 // indirect
github.com/imdario/mergo v0.3.8 // indirect
github.com/manifoldco/promptui v0.7.0
github.com/mattn/go-zglob v0.0.1
github.com/mitchellh/copystructure v1.0.0 // indirect
github.com/pkg/errors v0.9.1
github.com/urfave/cli/v2 v2.1.1

2
go.sum
View File

@ -67,6 +67,8 @@ github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaO
github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs=
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/go-zglob v0.0.1 h1:xsEx/XUoVlI6yXjqBK062zYhRTZltCNmYPx6v+8DNaY=
github.com/mattn/go-zglob v0.0.1/go.mod h1:9fxibJccNxU2cnpIKLRRFA7zX7qhkJIQWBb449FYHOo=
github.com/mitchellh/copystructure v1.0.0 h1:Laisrj+bAB6b/yJwB5Bt3ITZhGJdqmxquMKeZ+mmkFQ=
github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=

View File

@ -1,7 +1,7 @@
package command
import (
"log"
"fmt"
"net/url"
"os"
@ -32,6 +32,12 @@ func fromCommand() *cli.Command {
Usage: "The scaffold manifest `FILE`",
Value: "scaffold.yml",
},
&cli.StringSliceFlag{
Name: "ignore",
Aliases: []string{"i"},
Usage: "Ignore files matching pattern",
Value: cli.NewStringSlice(".git", ".git/**", "./.git/**/*"),
},
},
Action: fromAction,
}
@ -78,13 +84,13 @@ func fromAction(c *cli.Context) error {
return errors.Wrapf(err, "could not read manifest '%s'", manifestPath)
}
log.Println("Could not find scaffold manifest.")
fmt.Println("Could not find scaffold manifest.")
}
if manifestFile != nil {
defer manifestFile.Close()
log.Println("Loading template scaffold manifest...")
fmt.Println("Loading scaffold manifest.")
manifest, err := template.Load(manifestFile)
if err != nil {
@ -95,6 +101,7 @@ func fromAction(c *cli.Context) error {
return errors.Wrapf(err, "could not validate manifest '%s'", manifestPath)
}
fmt.Println("Prompting for templates variables.")
if err := template.Prompt(manifest, templateData); err != nil {
return errors.Wrap(err, "could not prompt for template data")
}
@ -102,9 +109,22 @@ func fromAction(c *cli.Context) error {
directory := c.String("directory")
return template.CopyDir(vfs, ".", directory, &template.Option{
TemplateData: templateData,
TemplateExt: ".gotpl",
IgnorePatterns: []string{manifestPath},
ignore := c.StringSlice("ignore")
ignore = append(ignore, manifestPath)
fmt.Println("Generating project tree.")
err = template.CopyDir(vfs, ".", directory, &template.Option{
TemplateData: templateData,
TemplateExt: ".gotpl",
IgnorePatterns: ignore,
PreferredStrategy: template.Undefined,
})
if err != nil {
return err
}
fmt.Println("Done.")
return nil
}

View File

@ -1,7 +1,7 @@
package project
import (
"log"
"fmt"
"net/url"
"os"
"strings"
@ -51,7 +51,7 @@ func (f *GitFetcher) Fetch(url *url.URL) (billy.Filesystem, error) {
url.Fragment = ""
}
log.Printf("Cloning repository '%s'...", url.String())
fmt.Printf("Cloning repository '%s'.\n", url.String())
repo, err := git.Clone(memory.NewStorage(), fs, &git.CloneOptions{
URL: url.String(),
@ -72,7 +72,7 @@ func (f *GitFetcher) Fetch(url *url.URL) (billy.Filesystem, error) {
return nil, errors.Wrap(err, "could not retrieve worktree")
}
log.Printf("Checking out branch '%s'...", branchName)
fmt.Printf("Checking out branch '%s'.\n", branchName)
err = worktree.Checkout(&git.CheckoutOptions{
Force: true,

View File

@ -1,9 +1,9 @@
package template
import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
@ -13,6 +13,7 @@ import (
"gopkg.in/src-d/go-billy.v4"
"github.com/Masterminds/sprig"
"github.com/mattn/go-zglob"
"github.com/pkg/errors"
)
@ -43,12 +44,12 @@ func CopyDir(vfs billy.Filesystem, baseDir string, dst string, opts *Option) err
}
for _, p := range opts.IgnorePatterns {
match, err := filepath.Match(p, srcPath)
match, err := zglob.Match(p, strings.TrimPrefix(srcPath, "./"))
if err != nil {
return errors.Wrap(err, "could not match ignored file")
}
if match {
log.Printf("Ignoring %s.", srcPath)
fmt.Printf("Ignoring %s.\n", srcPath)
return nil
}
}
@ -61,7 +62,7 @@ func CopyDir(vfs billy.Filesystem, baseDir string, dst string, opts *Option) err
dstPath := filepath.Join(dst, relSrcPath)
if info.IsDir() {
log.Printf("creating dir '%s'", dstPath)
fmt.Printf("Creating dir '%s'.\n", dstPath)
if err := os.MkdirAll(dstPath, 0755); err != nil {
return errors.Wrapf(err, "could not create directory '%s'", dstPath)
}
@ -89,6 +90,44 @@ func CopyFile(vfs billy.Filesystem, src, dst string, opts *Option) (err error) {
opts = &Option{}
}
stat, err := os.Stat(dst)
if err != nil && !os.IsNotExist(err) {
return err
}
// If dst exists
if stat != nil {
strategy := opts.PreferredStrategy
if opts.Unnattended && opts.PreferredStrategy == -1 {
strategy = defaultStrategy
}
if opts.PreferredStrategy == -1 {
var useAsPreferredStrategy bool
strategy, useAsPreferredStrategy, err = askOverwriteStrategy(dst)
if err != nil {
return errors.Wrap(err, "could not ask for overwrite strategy")
}
if useAsPreferredStrategy {
opts.PreferredStrategy = strategy
}
}
switch strategy {
case CopyAsDist:
fmt.Printf("Using '%s.dist' as destination file.\n", dst)
dst += ".dist"
case DoNotCopy:
fmt.Printf("Skipping existing file '%s'.\n", dst)
return nil
default:
return nil
}
}
if !strings.HasSuffix(src, opts.TemplateExt) {
return copyFile(vfs, src, dst)
}
@ -111,7 +150,7 @@ func CopyFile(vfs billy.Filesystem, src, dst string, opts *Option) (err error) {
dst = strings.TrimSuffix(dst, opts.TemplateExt)
log.Printf("templating file from '%s' to '%s'", src, dst)
fmt.Printf("Templating file from '%s' to '%s'.\n", src, dst)
out, err := os.Create(dst)
if err != nil {
@ -135,7 +174,7 @@ func CopyFile(vfs billy.Filesystem, src, dst string, opts *Option) (err error) {
}
func copyFile(vfs billy.Filesystem, src, dst string) (err error) {
log.Printf("copying file '%s' to '%s'", src, dst)
fmt.Printf("Copying file '%s' to '%s'.\n", src, dst)
in, err := vfs.Open(src)
if err != nil {

View File

@ -0,0 +1,7 @@
package template
import "github.com/pkg/errors"
var (
ErrUnexpectedOverwriteStrategy = errors.New("unexpected overwrite strategy")
)

View File

@ -1,7 +1,9 @@
package template
type Option struct {
IgnorePatterns []string
TemplateData Data
TemplateExt string
IgnorePatterns []string
TemplateData Data
TemplateExt string
Unnattended bool
PreferredStrategy Strategy
}

View File

@ -0,0 +1,62 @@
package template
import (
"fmt"
"github.com/manifoldco/promptui"
"github.com/pkg/errors"
)
type Strategy int
const (
Undefined Strategy = -1
Overwrite Strategy = iota
CopyAsDist
DoNotCopy
)
// nolint: gochecknoglobals
var defaultStrategy = CopyAsDist
func askOverwriteStrategy(dst string) (Strategy, bool, error) {
strategyPrompt := promptui.Select{
Label: fmt.Sprintf("The file '%s' already exists. What do you want to do ?", dst),
Items: []string{
"Leave <dest> file as is",
"Copy source file as '<dest>.dist'",
"Overwrite '<dest>' with source file",
},
}
strategySelectedIndex, _, err := strategyPrompt.Run()
if err != nil {
return -1, false, errors.Wrap(err, "could not prompt for overwrite strategy")
}
asPreferredStrategyPrompt := promptui.Select{
Label: "Do you want to apply this strategy to all files ?",
Items: []string{
"Yes",
"No",
},
}
preferredSelectedIndex, _, err := asPreferredStrategyPrompt.Run()
if err != nil {
return -1, false, errors.Wrap(err, "could not prompt for preferred strategy")
}
useAsPreferredStrategy := preferredSelectedIndex == 0
switch strategySelectedIndex {
case 0:
return DoNotCopy, useAsPreferredStrategy, nil
case 1:
return CopyAsDist, useAsPreferredStrategy, nil
case 2:
return Overwrite, useAsPreferredStrategy, nil
default:
return -1, false, ErrUnexpectedOverwriteStrategy
}
}

153
misc/script/gitea-release Executable file
View File

@ -0,0 +1,153 @@
#!/bin/bash
set -eo pipefail
GITEA_RELEASE_PROJECT=${GITEA_RELEASE_PROJECT}
GITEA_RELEASE_ORG=${GITEA_RELEASE_ORG}
GITEA_RELEASE_BASE_URL=${GITEA_BASE_URL:-https://forge.cadoles.com}
GITEA_RELEASE_USERNAME=${GITEA_RELEASE_USERNAME}
GITEA_RELEASE_PASSWORD=${GITEA_RELEASE_PASSWORD}
GITEA_RELEASE_VERSION=${GITEA_RELEASE_VERSION}
GITEA_RELEASE_COMMITISH_TARGET=${GITEA_RELEASE_COMMITISH_TARGET}
GITEA_RELEASE_IS_DRAFT=${GITEA_RELEASE_IS_DRAFT:-false}
GITEA_RELEASE_IS_PRERELEASE=${GITEA_RELEASE_IS_PRERELEASE:-true}
GITEA_RELEASE_BODY=${GITEA_RELEASE_BODY}
GITEA_RELEASE_ATTACHMENTS=${GITEA_RELEASE_ATTACHMENTS}
function check_dependencies {
assert_command_available 'curl'
assert_command_available 'jq'
}
function assert_command_available {
local command=$1
local command_path=$(which $command)
if [ -z "$command_path" ]; then
echo "The '$command' command could not be found. Please install it before using this script." 1>&2
exit 1
fi
}
function check_environment {
assert_environment GITEA_RELEASE_PROJECT
assert_environment GITEA_RELEASE_ORG
assert_environment GITEA_RELEASE_BASE_URL
}
function source_env_file {
if [ ! -f '.env' ]; then
return 0
fi
set -o allexport
source .env
set +o allexport
}
function assert_environment {
local name=$1
local value=${!name}
if [ -z "$value" ]; then
echo "The $"$name" environment variable is empty." 1>&2
exit 1
fi
}
function ask_credentials {
if [ -z "$GITEA_RELEASE_USERNAME" ]; then
echo -n "Username: "
read GITEA_RELEASE_USERNAME
fi
if [ -z "$GITEA_RELEASE_PASSWORD" ]; then
echo -n "Password: "
stty -echo
read GITEA_RELEASE_PASSWORD
stty echo
echo
fi
}
function retrieve_version {
if [ ! -z "$GITEA_RELEASE_VERSION" ]; then
return
fi
set +e
GITEA_RELEASE_VERSION=$(git describe --abbrev=0 --tags 2>/dev/null)
GITEA_RELEASE_VERSION=${GITEA_RELEASE_VERSION}
set -e
}
function retrieve_commitish_target {
if [ ! -z "$GITEA_RELEASE_COMMITISH_TARGET" ]; then
return
fi
GITEA_RELEASE_COMMITISH_TARGET=$(git log -n 1 --pretty="format:%h")
}
function create_release {
local payload={}
payload=$(json_set "$payload" body "\"$GITEA_RELEASE_BODY\"")
payload=$(json_set "$payload" draft $GITEA_RELEASE_IS_DRAFT)
payload=$(json_set "$payload" name "\"$GITEA_RELEASE_VERSION\"")
payload=$(json_set "$payload" prerelease $GITEA_RELEASE_IS_PRERELEASE)
payload=$(json_set "$payload" tag_name "\"${GITEA_RELEASE_VERSION:-$GITEA_RELEASE_COMMITISH_TARGET}\"")
payload=$(json_set "$payload" target_commitish "\"$GITEA_RELEASE_COMMITISH_TARGET\"")
gitea_api "/repos/$GITEA_RELEASE_ORG/$GITEA_RELEASE_PROJECT/releases" \
-H "Content-Type:application/json" \
-d "$payload"
}
function json_set {
local data=$1
local key=$2
local value=$3
echo $data | jq -cr --argjson v "$value" --arg k "$key" '.[$k] = $v'
}
function upload_release_attachments {
local release="$1"
local release_id=$(echo "$release" | jq -r .id)
if [ -z "$GITEA_RELEASE_ATTACHMENTS" ]; then
set +e
GITEA_RELEASE_ATTACHMENTS="$(ls release/*.{tar.gz,zip} 2>/dev/null)"
set -e
fi
for file in $GITEA_RELEASE_ATTACHMENTS; do
local filename=$(basename "$file")
gitea_api "/repos/$GITEA_RELEASE_ORG/$GITEA_RELEASE_PROJECT/releases/$release_id/assets?name=$filename" \
-H "Content-Type:multipart/form-data" \
-F "attachment=@$file"
done
}
function gitea_api {
local path=$1
local args=${@:2}
curl -L \
--fail \
-u "$GITEA_RELEASE_USERNAME:$GITEA_RELEASE_PASSWORD" \
${args} \
"$GITEA_RELEASE_BASE_URL/api/v1$path"
}
function main {
check_dependencies
source_env_file
check_environment
ask_credentials
retrieve_commitish_target
retrieve_version
local release=$(create_release)
upload_release_attachments "$release"
}
main