Skip to content

Commit

Permalink
fix: improve contents.type (#581)
Browse files Browse the repository at this point in the history
* fix: improve contents.type

improved jsonschema and added validations

Signed-off-by: Carlos A Becker <[email protected]>

* fix: config|noreplace

Signed-off-by: Carlos A Becker <[email protected]>

Signed-off-by: Carlos A Becker <[email protected]>
  • Loading branch information
caarlos0 authored Nov 24, 2022
1 parent b30373b commit 2918f99
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 6 deletions.
1 change: 1 addition & 0 deletions .github/workflows/generate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ on:
- 'www/*'
- 'cmd/*'
- 'internal/cmd/*'
- 'files/*'

jobs:
docs:
Expand Down
1 change: 1 addition & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ tasks:
- ./scripts/cmd_docs.sh
sources:
- cmd/*.go
- files/*.go
- nfpm.go
- ./scripts/cmd_docs.sh
- CONTRIBUTING.md
Expand Down
8 changes: 7 additions & 1 deletion apk/apk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ func exampleInfo() *nfpm.Info {
Destination: "/etc/fake/fake.conf",
Type: "config",
},
{
Source: "../testdata/whatever.conf",
Destination: "/etc/fake/fake2.conf",
Type: "config|noreplace",
},
{
Destination: "/var/log/whatever",
Type: "dir",
Expand Down Expand Up @@ -105,7 +110,7 @@ func TestCreateBuilderData(t *testing.T) {

require.NoError(t, builderData(tw))

require.Equal(t, 11784, buf.Len())
require.Equal(t, 13832, buf.Len())
}

func TestCombineToApk(t *testing.T) {
Expand Down Expand Up @@ -140,6 +145,7 @@ func TestDefaultWithArch(t *testing.T) {
"usr/share/doc/fake/fake.txt": "96c335dc28122b5f09a4cef74b156cd24c23784c",
"usr/local/bin/fake": "f46cece3eeb7d9ed5cb244d902775427be71492d",
"etc/fake/fake.conf": "96c335dc28122b5f09a4cef74b156cd24c23784c",
"etc/fake/fake2.conf": "96c335dc28122b5f09a4cef74b156cd24c23784c",
}
for _, arch := range []string{"386", "amd64"} {
arch := arch
Expand Down
5 changes: 5 additions & 0 deletions deb/deb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ func exampleInfo() *nfpm.Info {
Destination: "/etc/fake/fake.conf",
Type: "config",
},
{
Source: "../testdata/whatever.conf",
Destination: "/etc/fake/fake2.conf",
Type: "config|noreplace",
},
{
Destination: "/var/log/whatever",
Type: "dir",
Expand Down
8 changes: 5 additions & 3 deletions files/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
// of one file to copy into a package.
type Content struct {
Source string `yaml:"src,omitempty" json:"src,omitempty"`
Destination string `yaml:"dst,omitempty" json:"dst,omitempty"`
Type string `yaml:"type,omitempty" json:"type,omitempty"`
Destination string `yaml:"dst" json:"dst"`
Type string `yaml:"type,omitempty" json:"type,omitempty" jsonschema:"enum=symlink,enum=ghost,enum=config,enum=config|noreplace,enum=dir,enum=,default="`
Packager string `yaml:"packager,omitempty" json:"packager,omitempty"`
FileInfo *ContentFileInfo `yaml:"file_info,omitempty" json:"file_info,omitempty"`
}
Expand Down Expand Up @@ -150,7 +150,7 @@ func ExpandContentGlobs(contents Contents, disableGlobbing bool) (files Contents
// Ghost, symlinks and dirs need to be in the list, but dont glob
// them because they do not really exist
files = append(files, f.WithFileInfoDefaults())
default:
case "config", "config|noreplace", "file", "":
globbed, err = glob.Glob(f.Source, f.Destination, disableGlobbing)
if err != nil {
return nil, err
Expand All @@ -160,6 +160,8 @@ func ExpandContentGlobs(contents Contents, disableGlobbing bool) (files Contents
if err != nil {
return nil, err
}
default:
return files, fmt.Errorf("invalid file type: %s", f.Type)
}

}
Expand Down
45 changes: 45 additions & 0 deletions files/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,3 +382,48 @@ func TestDestEndsWithSlash(t *testing.T) {
require.Len(t, result, 1)
require.Equal(t, "foo/a.txt", result[0].Destination)
}

func TestInvalidFileType(t *testing.T) {
var config testStruct
dec := yaml.NewDecoder(strings.NewReader(`---
contents:
- src: testdata/globtest/**/*
dst: /bla
type: filr
`))
dec.KnownFields(true)
require.NoError(t, dec.Decode(&config))
_, err := files.ExpandContentGlobs(config.Contents, false)
require.EqualError(t, err, "invalid file type: filr")
}

func TestValidFileTypes(t *testing.T) {
var config testStruct
dec := yaml.NewDecoder(strings.NewReader(`---
contents:
- src: testdata/globtest/a.txt
dst: /f1.txt
- src: testdata/globtest/a.txt
dst: /f2.txt
type: file
- src: testdata/globtest/a.txt
dst: /f3.txt
type: config
- src: testdata/globtest/a.txt
dst: /f4.txt
type: config|noreplace
- src: testdata/globtest/a.txt
dst: /f5.txt
type: symlink
- src: testdata/globtest/a.txt
dst: /f6.txt
type: dir
- src: testdata/globtest/a.txt
dst: /f7.txt
type: ghost
`))
dec.KnownFields(true)
require.NoError(t, dec.Decode(&config))
_, err := files.ExpandContentGlobs(config.Contents, false)
require.NoError(t, err)
}
16 changes: 14 additions & 2 deletions www/docs/static/schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2918f99

Please sign in to comment.