Skip to content

Commit

Permalink
feat: add ansible scanner
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippHeuer committed Aug 29, 2024
1 parent 162762b commit 87b8cfb
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 16 deletions.
1 change: 1 addition & 0 deletions analyzerapi/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ const (
BuildSystemDotNet ProjectBuildSystem = "dotnet"
BuildSystemCargo ProjectBuildSystem = "cargo"
BuildSystemNix ProjectBuildSystem = "nix"
BuildSystemAnsible ProjectBuildSystem = "ansible"
)

type ProjectBuildSystemSyntax string
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
module github.com/cidverse/repoanalyzer

go 1.21
go 1.22.0

require (
github.com/cidverse/cidverseutils/filesystem v0.1.0
github.com/gosimple/slug v1.14.0
github.com/pelletier/go-toml/v2 v2.2.2
github.com/pelletier/go-toml/v2 v2.2.3
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06
github.com/stretchr/testify v1.9.0
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842
golang.org/x/exp v0.0.0-20240823005443-9b4947da3948
golang.org/x/mod v0.20.0
)

require (
github.com/charlievieth/fastwalk v1.0.3 // indirect
github.com/charlievieth/fastwalk v1.0.8 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gosimple/unidecode v1.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand Down
18 changes: 6 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/charlievieth/fastwalk v1.0.3 h1:eNWFaNPe5srPqQ5yyDbhAf11paeZaHWcihRhpuYFfSg=
github.com/charlievieth/fastwalk v1.0.3/go.mod h1:JSfglY/gmL/rqsUS1NCsJTocB5n6sSl9ApAqif4CUbs=
github.com/charlievieth/fastwalk v1.0.8 h1:uaoH6cAKSk73aK7aKXqs0+bL+J3Txzd3NGH8tRXgHko=
github.com/charlievieth/fastwalk v1.0.8/go.mod h1:yGy1zbxog41ZVMcKA/i8ojXLFsuayX5VvwhQVoj9PBI=
github.com/cidverse/cidverseutils/filesystem v0.1.0 h1:0WF4gBtThvRMWUsoGtK1MT5O6yE5Ql6H2x6RkMLQoc8=
github.com/cidverse/cidverseutils/filesystem v0.1.0/go.mod h1:rw4oLD+q/7HCMkQUooxVz4He06ORyoAn2cXLFqpBinQ=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -9,24 +9,18 @@ github.com/gosimple/slug v1.14.0 h1:RtTL/71mJNDfpUbCOmnf/XFkzKRtD6wL6Uy+3akm4Es=
github.com/gosimple/slug v1.14.0/go.mod h1:UiRaFH+GEilHstLUmcBgWcI42viBN7mAb818JrYOeFQ=
github.com/gosimple/unidecode v1.0.1 h1:hZzFTMMqSswvf0LBJZCZgThIZrpDHFXux9KeGmn6T/o=
github.com/gosimple/unidecode v1.0.1/go.mod h1:CP0Cr1Y1kogOtx0bJblKzsVWrqYaqfNOnHzpgWw4Awc=
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M=
github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06 h1:OkMGxebDjyw0ULyrTYWeN0UNCCkmCWfjPnIA2W6oviI=
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06/go.mod h1:+ePHsJ1keEjQtpvf9HHw0f4ZeJ0TLRsxhunSI2hYJSs=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 h1:vr/HnozRka3pE4EsMEg1lgkXJkTFJCVUX+S/ZT6wYzM=
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842/go.mod h1:XtvwrStGgqGPLc4cjQfWqZHG1YFdYs6swckp8vpsjnc=
golang.org/x/exp v0.0.0-20240823005443-9b4947da3948 h1:kx6Ds3MlpiUHKj7syVnbp57++8WpuKPcR5yjLBjvLEA=
golang.org/x/exp v0.0.0-20240823005443-9b4947da3948/go.mod h1:akd2r19cwCdwSwWeIdzYQGa/EZZyqcOdwWiwj5L5eKQ=
golang.org/x/mod v0.20.0 h1:utOm6MM3R3dnawAiJgn0y+xvuYRsm1RKM/4giyfDgV0=
golang.org/x/mod v0.20.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down
43 changes: 43 additions & 0 deletions modules/ansible/ansible.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package ansible

import (
"path/filepath"
"strings"

"github.com/cidverse/repoanalyzer/analyzerapi"
"github.com/gosimple/slug"
)

type Analyzer struct{}

func (a Analyzer) GetName() string {
return "container"
}

func (a Analyzer) Scan(ctx analyzerapi.AnalyzerContext) []*analyzerapi.ProjectModule {
var result []*analyzerapi.ProjectModule

for _, file := range ctx.FilesByExtension["yml"] {
filename := filepath.Base(file)

if filename == "playbook.yml" || strings.HasSuffix(filename, "-playbook.yml") {
module := analyzerapi.ProjectModule{
RootDirectory: ctx.ProjectDir,
Directory: filepath.Dir(file),
Name: filepath.Base(filepath.Dir(file)),
Slug: slug.Make(filepath.Base(filepath.Dir(file))),
Discovery: []analyzerapi.ProjectModuleDiscovery{{File: file}},
BuildSystem: analyzerapi.BuildSystemAnsible,
BuildSystemSyntax: analyzerapi.BuildSystemSyntaxDefault,
Language: nil,
Dependencies: nil,
Submodules: nil,
Files: ctx.Files,
FilesByExtension: ctx.FilesByExtension,
}
analyzerapi.AddModuleToResult(&result, &module)
}
}

return result
}
34 changes: 34 additions & 0 deletions modules/ansible/ansible_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package ansible

import (
"os"
"path/filepath"
"testing"

"github.com/cidverse/repoanalyzer/analyzerapi"
"github.com/stretchr/testify/assert"
)

func TestAnsibleAnalyzer_Analyze(t *testing.T) {
cwd, err := os.Getwd()
assert.NoError(t, err)

ctx := analyzerapi.GetAnalyzerContext(filepath.Join(filepath.Dir(cwd), "..", "testdata", "ansible"))
analyzer := Analyzer{}
result := analyzer.Scan(ctx)

// module
assert.Len(t, result, 1)
assert.Len(t, result[0].Discovery, 1)
assert.Equal(t, "playbook-a", result[0].Name)
assert.Equal(t, analyzerapi.BuildSystemAnsible, result[0].BuildSystem)
assert.Equal(t, analyzerapi.BuildSystemSyntaxDefault, result[0].BuildSystemSyntax)

// submodule
assert.Len(t, result[0].Submodules, 0)

// print result
for i, item := range result {
t.Logf("result[%d]: %+v", i, *item)
}
}
1 change: 1 addition & 0 deletions modules/gomod/gomod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func TestGoModAnalyzer_Analyze(t *testing.T) {

// module
assert.Len(t, result, 1)
assert.Len(t, result[0].Discovery, 1)
assert.Equal(t, "github.com/dummymodule", result[0].Name)
assert.Equal(t, analyzerapi.BuildSystemGoMod, result[0].BuildSystem)
assert.Equal(t, analyzerapi.BuildSystemSyntaxDefault, result[0].BuildSystemSyntax)
Expand Down
18 changes: 18 additions & 0 deletions testdata/ansible/playbook-a/playbook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
- hosts: all
tags:
- example
roles:
- role: facts
vars:
# user
user: "{{ lookup('env','USER') }}"
# proxy
proxy_env:
http_proxy: "{{ lookup('env','http_proxy') }}"
https_proxy: "{{ lookup('env','https_proxy') }}"
no_proxy: "{{ lookup('env','no_proxy') }}"
# paths
paths:
bin: "{{ ansible_env.HOME }}/.local/bin"
share: "{{ ansible_env.HOME }}/.local/share"

0 comments on commit 87b8cfb

Please sign in to comment.