From 451b357e4093644d4af65bf9f733713bb40c6da5 Mon Sep 17 00:00:00 2001 From: Matt McKenzie Date: Mon, 27 Dec 2021 18:22:03 -0800 Subject: [PATCH] Make linuxdb.yaml an embedded resource --- cmd/fs.go | 10 ---------- cmd/linuxdb.go | 10 +++++----- 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/cmd/fs.go b/cmd/fs.go index 5854539d0..030c6546f 100644 --- a/cmd/fs.go +++ b/cmd/fs.go @@ -148,16 +148,6 @@ func (fs *FSHelper) LocalDir(dir string) (*Dir, error) { }, err } -// LoadRelativeFile loads the given file relative to the caller's directory -func (fs *FSHelper) LoadRelativeFile(relativePath string) ([]byte, error) { - _, filename, _, _ := runtime.Caller(0) - fullPath, err := filepath.Abs(filepath.Join(path.Dir(filename), relativePath)) - if err != nil { - return nil, err - } - return os.ReadFile(fullPath) -} - // GetSubdirs will return a list of FQPs to subdirectories in the given directory func (d *Dir) GetSubdirs() (map[string]string, error) { diff --git a/cmd/linuxdb.go b/cmd/linuxdb.go index fbea6b686..ca7c368e3 100644 --- a/cmd/linuxdb.go +++ b/cmd/linuxdb.go @@ -1,11 +1,15 @@ package cmd import ( + _ "embed" "log" "gopkg.in/yaml.v3" ) +//go:embed linuxdb.yaml +var LinuxDBYaml []byte + // LinuxDB is the database for linux distribution data. type LinuxDB struct { Distributions map[string]*Distribution `yaml:"distributions"` @@ -78,14 +82,10 @@ func (l *LinuxDB) GetDistro(distro string) *Distribution { // NewLinuxDB creates a new LinuxDB instance from the bundled // linuxdb.yaml file. func NewLinuxDB() *LinuxDB { - data, err := fs.LoadRelativeFile("./linuxdb.yaml") - if err != nil { - log.Fatal("Could not load linuxdb.yaml") - } result := LinuxDB{ Distributions: make(map[string]*Distribution), } - err = result.ImportData(data) + err := result.ImportData(LinuxDBYaml) if err != nil { log.Fatal(err) }