Class: LicenseAuto::Golang

Inherits:
PackageManager show all
Defined in:
lib/license_auto/package_manager/golang.rb

Constant Summary collapse

LANGUAGE =
'Golang'

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PackageManager

#check_cli, #dependency_file_path_names

Constructor Details

#initialize(path) ⇒ Golang

Returns a new instance of Golang.



12
13
14
# File 'lib/license_auto/package_manager/golang.rb', line 12

def initialize(path)
  super(path)
end

Class Method Details

.check_cliObject



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/license_auto/package_manager/golang.rb', line 137

def self.check_cli
  bash_cmd = "go version"
  # LicenseAuto.logger.debug(bash_cmd)
  stdout_str, stderr_str, _status = Open3.capture3(bash_cmd)
  golang_version = /1\.6/

  if not stderr_str.empty?
    LicenseAuto.logger.error(stderr_str)
    return false
  elsif not stdout_str =~ golang_version
    error = "Golang version: #{stdout_str} not satisfied: #{golang_version}"
    LicenseAuto.logger.error(error)
    return false
  end

  return true
end

Instance Method Details

#dependency_file_patternObject

Fake



17
18
19
# File 'lib/license_auto/package_manager/golang.rb', line 17

def dependency_file_pattern
  /#{@path}\/.*\.go$/
end

#fetch_remote_latest_sha(repo_url) ⇒ clone_url, latest_sha

Returns:

  • (clone_url, latest_sha)


47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/license_auto/package_manager/golang.rb', line 47

def fetch_remote_latest_sha(repo_url)
  matcher = Matcher::SourceURL.new(repo_url)
  github_matched = matcher.match_github_resource
  if github_matched
    github = GithubCom.new({}, github_matched[:owner], github_matched[:repo])
    latest_sha = github.latest_commit.sha
    # LicenseAuto.logger.debug(latest_sha)
    [github.url, latest_sha]
  else
    [repo_url, nil]
  end
end

#filter_deps(listed_content) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/license_auto/package_manager/golang.rb', line 60

def filter_deps(listed_content)

  dep_keys = ['Deps', 'Imports', 'TestImports', 'XTestImports']
  deps = dep_keys.map {|key|
    listed_content[key]
  }.flatten.compact

  deps = Set.new(deps)
  deps.reject {|dep|
    bool = GOLANG_STD_LIBS.include?(dep)
    # LicenseAuto.logger.debug("#{dep}, #{bool}")
    bool
  }.map {|dep|
    host, owner, repo, _subdir = dep.split('/')
    [host, owner, repo].join('/')
  }
end

#list_contentObject

Returns {

"Dir": "/Users/mic/vm/test-branch",
"ImportPath": "_/Users/mic/vm/test-branch",
"Name": "main",
"Stale": true,
"GoFiles": [
    "main.go"
],
"Imports": [
    "fmt",
    "github.com/astaxie/beego",
    "math/rand"
],
"Deps": [
    "errors",
    "fmt",
    "github.com/astaxie/beego",
    "internal/race",
    "io",
    "math",
    "math/rand",
    "os",
    "reflect",
    "runtime",
    "runtime/internal/atomic",
    "runtime/internal/sys",
    "strconv",
    "sync",
    "sync/atomic",
    "syscall",
    "time",
    "unicode/utf8",
    "unsafe"
],
"Incomplete": true,
"DepsErrors": [
    {
        "ImportStack": [
            ".",
            "github.com/astaxie/beego"
        ],
        "Pos": "main.go:9:2",
        "Err": "cannot find package \"github.com/astaxie/beego\" in any of:\n\t/usr/local/Cellar/go/1.6/libexec/src/github.com/astaxie/beego (from $GOROOT)\n\t($GOPATH not set)"
    }
]

}.

Returns:

  • {

    "Dir": "/Users/mic/vm/test-branch",
    "ImportPath": "_/Users/mic/vm/test-branch",
    "Name": "main",
    "Stale": true,
    "GoFiles": [
        "main.go"
    ],
    "Imports": [
        "fmt",
        "github.com/astaxie/beego",
        "math/rand"
    ],
    "Deps": [
        "errors",
        "fmt",
        "github.com/astaxie/beego",
        "internal/race",
        "io",
        "math",
        "math/rand",
        "os",
        "reflect",
        "runtime",
        "runtime/internal/atomic",
        "runtime/internal/sys",
        "strconv",
        "sync",
        "sync/atomic",
        "syscall",
        "time",
        "unicode/utf8",
        "unsafe"
    ],
    "Incomplete": true,
    "DepsErrors": [
        {
            "ImportStack": [
                ".",
                "github.com/astaxie/beego"
            ],
            "Pos": "main.go:9:2",
            "Err": "cannot find package \"github.com/astaxie/beego\" in any of:\n\t/usr/local/Cellar/go/1.6/libexec/src/github.com/astaxie/beego (from $GOROOT)\n\t($GOPATH not set)"
        }
    ]
    

    }



129
130
131
132
133
134
135
# File 'lib/license_auto/package_manager/golang.rb', line 129

def list_content
  Dir.chdir(@path) do
    cmd = 'go list -json ./...'
    stdout_str, stderr_str, status = Open3.capture3(cmd)
    Hashie::Mash.new(JSON.parse(stdout_str)) if stdout_str.length > 0
  end
end

#parse_dependenciesObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/license_auto/package_manager/golang.rb', line 21

def parse_dependencies
  content = list_content
  if content.nil?
    LicenseAuto.logger.info("Golang dependencies not exist")
    return []
  else
    deps = filter_deps(content)
    LicenseAuto.logger.debug(deps)
    [
      {
          dep_file: nil,
          deps: deps.map {|dep|
                  remote, latest_sha = fetch_remote_latest_sha(dep)
                  {
                      name: dep,
                      version: latest_sha,
                      remote: remote
                  }
          }
      }
    ]
  end
  # LicenseAuto.logger.debug(JSON.pretty_generate(dep_files))
end

#uniform_urlObject



78
79
80
# File 'lib/license_auto/package_manager/golang.rb', line 78

def uniform_url

end