fix linting errors
This commit is contained in:
parent
f2e451e668
commit
08e8ca1aa7
10
README.md
10
README.md
@ -24,7 +24,7 @@ make build
|
|||||||
|
|
||||||
# Update Resource state
|
# Update Resource state
|
||||||
|
|
||||||
`cli -resource-file decl-runner.yaml`
|
`decl -resource-file decl-runner.yaml`
|
||||||
|
|
||||||
# Read resource state
|
# Read resource state
|
||||||
|
|
||||||
@ -36,6 +36,8 @@ Read the state of an existing resource (URI) and generate a YAML representation
|
|||||||
|
|
||||||
Resources:
|
Resources:
|
||||||
|
|
||||||
* [file](examples/file.yaml)
|
* [file](examples/file.yaml) [schema](internal/resource/file.jsonschema)
|
||||||
* [user](examples/user.yaml)
|
* [user](examples/user.yaml) [schema](internal/resource/user.jsonschema)
|
||||||
* [container](examples/container.yaml)
|
* [package](examples/package.yaml) [schema](internal/resource/package.jsonschema)
|
||||||
|
* [container](examples/container.yaml) [schema](internal/resource/container.jsonschema)
|
||||||
|
* [network_route](examples/network_route.yaml) [schema](internal/resource/network_route.jsonschema)
|
||||||
|
@ -60,9 +60,13 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if *resourceUri != "" {
|
if *resourceUri != "" {
|
||||||
slog.Info("importing resource: %s\n", *resourceUri)
|
slog.Info("importing resource", "resource", *resourceUri)
|
||||||
d.AddResource(*resourceUri)
|
if addResourceErr := d.AddResource(*resourceUri); addResourceErr != nil {
|
||||||
|
log.Fatal(addResourceErr)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
d.Generate(os.Stdout)
|
if documentGenerateErr := d.Generate(os.Stdout); documentGenerateErr != nil {
|
||||||
|
log.Fatal(documentGenerateErr)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -62,8 +62,8 @@ func (d *Declaration) SetURI(uri string) error {
|
|||||||
panic("unknown resource")
|
panic("unknown resource")
|
||||||
}
|
}
|
||||||
d.Type = TypeName(d.Attributes.Type())
|
d.Type = TypeName(d.Attributes.Type())
|
||||||
d.Attributes.Read(context.Background()) // fix context
|
_,e := d.Attributes.Read(context.Background()) // fix context
|
||||||
return nil
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Declaration) UnmarshalYAML(value *yaml.Node) error {
|
func (d *Declaration) UnmarshalYAML(value *yaml.Node) error {
|
||||||
|
@ -55,7 +55,8 @@ func TestNewResourceDeclarationType(t *testing.T) {
|
|||||||
resourceDeclaration := NewDeclaration()
|
resourceDeclaration := NewDeclaration()
|
||||||
assert.NotEqual(t, nil, resourceDeclaration)
|
assert.NotEqual(t, nil, resourceDeclaration)
|
||||||
|
|
||||||
resourceDeclaration.LoadDecl(decl)
|
e := resourceDeclaration.LoadDecl(decl)
|
||||||
|
assert.Nil(t, e)
|
||||||
assert.Equal(t, TypeName("file"), resourceDeclaration.Type)
|
assert.Equal(t, TypeName("file"), resourceDeclaration.Type)
|
||||||
assert.NotEqual(t, nil, resourceDeclaration.Attributes)
|
assert.NotEqual(t, nil, resourceDeclaration.Attributes)
|
||||||
}
|
}
|
||||||
|
@ -66,8 +66,12 @@ func (d *Document) Apply() error {
|
|||||||
|
|
||||||
func (d *Document) Generate(w io.Writer) error {
|
func (d *Document) Generate(w io.Writer) error {
|
||||||
e := NewYAMLEncoder(w)
|
e := NewYAMLEncoder(w)
|
||||||
e.Encode(d)
|
err := e.Encode(d);
|
||||||
return e.Close()
|
if err == nil {
|
||||||
|
return e.Close()
|
||||||
|
}
|
||||||
|
e.Close()
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Document) AddResourceDeclaration(resourceType string, resourceDeclaration Resource) {
|
func (d *Document) AddResourceDeclaration(resourceType string, resourceDeclaration Resource) {
|
||||||
@ -81,7 +85,10 @@ func (d *Document) AddResource(uri string) error {
|
|||||||
//parsedResourceURI, e := url.Parse(uri)
|
//parsedResourceURI, e := url.Parse(uri)
|
||||||
//if e == nil {
|
//if e == nil {
|
||||||
decl := NewDeclaration()
|
decl := NewDeclaration()
|
||||||
decl.SetURI(uri)
|
if e := decl.SetURI(uri); e != nil {
|
||||||
|
return e
|
||||||
|
}
|
||||||
|
|
||||||
d.ResourceDecls = append(d.ResourceDecls, *decl)
|
d.ResourceDecls = append(d.ResourceDecls, *decl)
|
||||||
//}
|
//}
|
||||||
return nil
|
return nil
|
||||||
|
@ -107,7 +107,8 @@ resources:
|
|||||||
assert.NotNil(t, f)
|
assert.NotNil(t, f)
|
||||||
|
|
||||||
f.(*File).Path = filepath.Join(TempDir, "foo.txt")
|
f.(*File).Path = filepath.Join(TempDir, "foo.txt")
|
||||||
f.(*File).Read(ctx)
|
_,readErr := f.(*File).Read(ctx)
|
||||||
|
assert.Nil(t, readErr)
|
||||||
d.AddResourceDeclaration("file", f)
|
d.AddResourceDeclaration("file", f)
|
||||||
ey := d.Generate(&documentYaml)
|
ey := d.Generate(&documentYaml)
|
||||||
assert.Equal(t, nil, ey)
|
assert.Equal(t, nil, ey)
|
||||||
@ -123,11 +124,12 @@ func TestDocumentAddResource(t *testing.T) {
|
|||||||
|
|
||||||
d := NewDocument()
|
d := NewDocument()
|
||||||
assert.NotNil(t, d)
|
assert.NotNil(t, d)
|
||||||
d.AddResource(fmt.Sprintf("file://%s", file))
|
e := d.AddResource(fmt.Sprintf("file://%s", file))
|
||||||
|
assert.Nil(t, e)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDocumentJSON(t *testing.T) {
|
func TestDocumentJSON(t *testing.T) {
|
||||||
document := fmt.Sprintf(`
|
document := `
|
||||||
---
|
---
|
||||||
resources:
|
resources:
|
||||||
- type: user
|
- type: user
|
||||||
@ -137,7 +139,7 @@ resources:
|
|||||||
group: "10022"
|
group: "10022"
|
||||||
home: "/home/testuser"
|
home: "/home/testuser"
|
||||||
state: present
|
state: present
|
||||||
`)
|
`
|
||||||
d := NewDocument()
|
d := NewDocument()
|
||||||
assert.NotNil(t, d)
|
assert.NotNil(t, d)
|
||||||
docReader := strings.NewReader(document)
|
docReader := strings.NewReader(document)
|
||||||
@ -158,7 +160,7 @@ func TestDocumentJSONSchema(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestDocumentYAML(t *testing.T) {
|
func TestDocumentYAML(t *testing.T) {
|
||||||
document := fmt.Sprintf(`
|
document := `
|
||||||
---
|
---
|
||||||
resources:
|
resources:
|
||||||
- type: user
|
- type: user
|
||||||
@ -168,7 +170,7 @@ resources:
|
|||||||
group: "10022"
|
group: "10022"
|
||||||
home: "/home/testuser"
|
home: "/home/testuser"
|
||||||
state: present
|
state: present
|
||||||
`)
|
`
|
||||||
d := NewDocument()
|
d := NewDocument()
|
||||||
assert.NotNil(t, d)
|
assert.NotNil(t, d)
|
||||||
docReader := strings.NewReader(document)
|
docReader := strings.NewReader(document)
|
||||||
|
@ -43,7 +43,8 @@ func TestCreateExec(t *testing.T) {
|
|||||||
func TestExecSetURI(t *testing.T) {
|
func TestExecSetURI(t *testing.T) {
|
||||||
x := NewExec()
|
x := NewExec()
|
||||||
assert.NotNil(t, x)
|
assert.NotNil(t, x)
|
||||||
x.SetURI("exec://" + "12345_key")
|
e := x.SetURI("exec://" + "12345_key")
|
||||||
|
assert.Nil(t, e)
|
||||||
assert.Equal(t, "exec", x.Type())
|
assert.Equal(t, "exec", x.Type())
|
||||||
assert.Equal(t, "12345_key", x.Id)
|
assert.Equal(t, "12345_key", x.Id)
|
||||||
}
|
}
|
||||||
|
@ -69,10 +69,12 @@ func (f *File) URI() string {
|
|||||||
|
|
||||||
func (f *File) SetURI(uri string) error {
|
func (f *File) SetURI(uri string) error {
|
||||||
resourceUri, e := url.Parse(uri)
|
resourceUri, e := url.Parse(uri)
|
||||||
if resourceUri.Scheme == "file" {
|
if e == nil {
|
||||||
f.Path, e = filepath.Abs(filepath.Join(resourceUri.Hostname(), resourceUri.RequestURI()))
|
if resourceUri.Scheme == "file" {
|
||||||
} else {
|
f.Path, e = filepath.Abs(filepath.Join(resourceUri.Hostname(), resourceUri.RequestURI()))
|
||||||
e = fmt.Errorf("%w: %s is not a file", ErrInvalidResourceURI, uri)
|
} else {
|
||||||
|
e = fmt.Errorf("%w: %s is not a file", ErrInvalidResourceURI, uri)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
@ -110,7 +112,9 @@ func (f *File) Apply() error {
|
|||||||
return linkErr
|
return linkErr
|
||||||
}
|
}
|
||||||
case DirectoryFile:
|
case DirectoryFile:
|
||||||
os.MkdirAll(f.Path, os.FileMode(mode))
|
if mkdirErr := os.MkdirAll(f.Path, os.FileMode(mode)); mkdirErr != nil {
|
||||||
|
return mkdirErr
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
fallthrough
|
fallthrough
|
||||||
case RegularFile:
|
case RegularFile:
|
||||||
@ -201,8 +205,10 @@ func (f *File) ReadStat() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (f *File) Read(ctx context.Context) ([]byte, error) {
|
func (f *File) Read(ctx context.Context) ([]byte, error) {
|
||||||
f.NormalizePath()
|
if normalizePathErr := f.NormalizePath(); normalizePathErr != nil {
|
||||||
|
return nil, normalizePathErr
|
||||||
|
}
|
||||||
|
|
||||||
statErr := f.ReadStat()
|
statErr := f.ReadStat()
|
||||||
if statErr != nil {
|
if statErr != nil {
|
||||||
return nil, statErr
|
return nil, statErr
|
||||||
|
@ -57,7 +57,8 @@ func TestReadFile(t *testing.T) {
|
|||||||
testFile := NewFile()
|
testFile := NewFile()
|
||||||
e := testFile.LoadDecl(decl)
|
e := testFile.LoadDecl(decl)
|
||||||
assert.Equal(t, nil, e)
|
assert.Equal(t, nil, e)
|
||||||
testFile.Apply()
|
applyErr := testFile.Apply()
|
||||||
|
assert.Nil(t, applyErr)
|
||||||
|
|
||||||
f := NewFile()
|
f := NewFile()
|
||||||
assert.NotEqual(t, nil, f)
|
assert.NotEqual(t, nil, f)
|
||||||
@ -184,7 +185,8 @@ func TestFileSetURI(t *testing.T) {
|
|||||||
file, _ := filepath.Abs(filepath.Join(TempDir, "testuri.txt"))
|
file, _ := filepath.Abs(filepath.Join(TempDir, "testuri.txt"))
|
||||||
f := NewFile()
|
f := NewFile()
|
||||||
assert.NotNil(t, f)
|
assert.NotNil(t, f)
|
||||||
f.SetURI("file://" + file)
|
e := f.SetURI("file://" + file)
|
||||||
|
assert.Nil(t, e)
|
||||||
assert.Equal(t, "file", f.Type())
|
assert.Equal(t, "file", f.Type())
|
||||||
assert.Equal(t, file, f.Path)
|
assert.Equal(t, file, f.Path)
|
||||||
}
|
}
|
||||||
@ -235,11 +237,14 @@ func TestFileReadStat(t *testing.T) {
|
|||||||
l.Target = linkTargetFile
|
l.Target = linkTargetFile
|
||||||
l.State = "present"
|
l.State = "present"
|
||||||
|
|
||||||
l.Apply()
|
applyErr := l.Apply()
|
||||||
l.ReadStat()
|
assert.Nil(t, applyErr)
|
||||||
|
readStatErr := l.ReadStat()
|
||||||
|
assert.Nil(t, readStatErr)
|
||||||
|
|
||||||
testRead := NewFile()
|
testRead := NewFile()
|
||||||
testRead.Path = link
|
testRead.Path = link
|
||||||
testRead.Read(ctx)
|
_,testReadErr := testRead.Read(ctx)
|
||||||
|
assert.Nil(t, testReadErr)
|
||||||
assert.Equal(t, linkTargetFile, testRead.Target)
|
assert.Equal(t, linkTargetFile, testRead.Target)
|
||||||
}
|
}
|
||||||
|
@ -50,7 +50,8 @@ func TestReadNetworkRoute(t *testing.T) {
|
|||||||
testRoute := NewNetworkRoute()
|
testRoute := NewNetworkRoute()
|
||||||
e := testRoute.LoadDecl(declarationAttributes)
|
e := testRoute.LoadDecl(declarationAttributes)
|
||||||
assert.Equal(t, nil, e)
|
assert.Equal(t, nil, e)
|
||||||
testRoute.Apply()
|
testRouteErr := testRoute.Apply()
|
||||||
|
assert.Nil(t, testRouteErr)
|
||||||
r, e := testRoute.Read(ctx)
|
r, e := testRoute.Read(ctx)
|
||||||
|
|
||||||
assert.Nil(t, e)
|
assert.Nil(t, e)
|
||||||
|
@ -53,7 +53,8 @@ func TestSchemaValidate(t *testing.T) {
|
|||||||
testFile := NewFile()
|
testFile := NewFile()
|
||||||
e := testFile.LoadDecl(decl)
|
e := testFile.LoadDecl(decl)
|
||||||
assert.Equal(t, nil, e)
|
assert.Equal(t, nil, e)
|
||||||
testFile.Apply()
|
fileApplyErr := testFile.Apply()
|
||||||
|
assert.Nil(t, fileApplyErr)
|
||||||
|
|
||||||
jsonDoc, jsonErr := json.Marshal(testFile)
|
jsonDoc, jsonErr := json.Marshal(testFile)
|
||||||
assert.Nil(t, jsonErr)
|
assert.Nil(t, jsonErr)
|
||||||
|
19
internal/resource/schemas/package.jsonschema
Normal file
19
internal/resource/schemas/package.jsonschema
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
{
|
||||||
|
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||||
|
"title": "package",
|
||||||
|
"type": "object",
|
||||||
|
"required": [ "name", "type" ],
|
||||||
|
"properties": {
|
||||||
|
"name": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"version": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"type": {
|
||||||
|
"type": "string",
|
||||||
|
"description": "package type",
|
||||||
|
"enum": [ "rpm", "deb", "yum", "dnf", "apt", "pip", "go" ]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -64,10 +64,14 @@ func (u *User) Apply() error {
|
|||||||
if _, pathErr := exec.LookPath("useradd"); pathErr != nil {
|
if _, pathErr := exec.LookPath("useradd"); pathErr != nil {
|
||||||
if _, addUserPathErr := exec.LookPath("adduser"); addUserPathErr == nil {
|
if _, addUserPathErr := exec.LookPath("adduser"); addUserPathErr == nil {
|
||||||
userCommandName = "adduser"
|
userCommandName = "adduser"
|
||||||
u.AddUserCommand(&args)
|
if addUserCommandErr := u.AddUserCommand(&args); addUserCommandErr != nil {
|
||||||
|
return addUserCommandErr
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
u.UserAddCommand(&args)
|
if userAddCommandErr := u.UserAddCommand(&args); userAddCommandErr != nil {
|
||||||
|
return userAddCommandErr
|
||||||
|
}
|
||||||
}
|
}
|
||||||
args = append(args, u.Name)
|
args = append(args, u.Name)
|
||||||
cmd := exec.Command(userCommandName, args...)
|
cmd := exec.Command(userCommandName, args...)
|
||||||
|
Loading…
Reference in New Issue
Block a user