diff --git a/apk/apk.go b/apk/apk.go index b462c5b..f7c24af 100644 --- a/apk/apk.go +++ b/apk/apk.go @@ -12,56 +12,6 @@ type Apk struct { apkDir string } -func (a *Apk) apkBuild(artifactsPath string) error { - err := utils.Exec(a.apkDir, "abuild-keygen", "-n", "-a") - if err != nil { - return err - } - - err = utils.Exec(a.apkDir, "abuild", "-F", "-K", "-P", artifactsPath) - if err != nil { - return err - } - - return err -} - -func (a *Apk) Prepare(makeDepends []string) error { - args := []string{ - "add", - } - - err := a.PKGBUILD.GetDepends("apk", args, makeDepends) - if err != nil { - return err - } - - return err -} - -func (a *Apk) Update() error { - err := a.PKGBUILD.GetUpdates("apk", "update") - if err != nil { - return err - } - - return err -} - -func (a *Apk) makePackerDir() error { - err := utils.ExistsMakeDir(a.apkDir) - if err != nil { - return err - } - - err = utils.ExistsMakeDir(a.apkDir + "/pkg/" + a.PKGBUILD.PkgName) - if err != nil { - return err - } - - return err -} - func (a *Apk) Build(artifactsPath string) error { a.apkDir = filepath.Join(a.PKGBUILD.StartDir, "apk") @@ -115,6 +65,19 @@ func (a *Apk) Install(artifactsPath string) error { return err } +func (a *Apk) Prepare(makeDepends []string) error { + args := []string{ + "add", + } + + err := a.PKGBUILD.GetDepends("apk", args, makeDepends) + if err != nil { + return err + } + + return err +} + func (a *Apk) PrepareEnvironment(golang bool) error { var err error @@ -136,3 +99,40 @@ func (a *Apk) PrepareEnvironment(golang bool) error { return err } + +func (a *Apk) Update() error { + err := a.PKGBUILD.GetUpdates("apk", "update") + if err != nil { + return err + } + + return err +} + +func (a *Apk) apkBuild(artifactsPath string) error { + err := utils.Exec(a.apkDir, "abuild-keygen", "-n", "-a") + if err != nil { + return err + } + + err = utils.Exec(a.apkDir, "abuild", "-F", "-K", "-P", artifactsPath) + if err != nil { + return err + } + + return err +} + +func (a *Apk) makePackerDir() error { + err := utils.ExistsMakeDir(a.apkDir) + if err != nil { + return err + } + + err = utils.ExistsMakeDir(a.apkDir + "/pkg/" + a.PKGBUILD.PkgName) + if err != nil { + return err + } + + return err +} diff --git a/apk/constants.go b/apk/constants.go index ac400ee..dec05a3 100644 --- a/apk/constants.go +++ b/apk/constants.go @@ -4,38 +4,6 @@ var buildEnvironmentDeps = []string{ "alpine-sdk", } -const specFile = ` -{{- /* Mandatory fields */ -}} -pkgname={{.PkgName}} -{{- if .Epoch}} -epoch={{.Epoch}} -{{- end }} -pkgver={{.PkgVer}} -pkgrel={{.PkgRel}} -pkgdesc="{{.PkgDesc}}" -arch="all" -{{- with .Depends}} -depends="{{join .}}" -{{- end }} -{{- with .Conflicts}} -conflicts=({{join .}}) -{{- end }} -{{- if .URL}} -url="{{.URL}}" -{{- end }} -{{- if .Install}} -install={{.PkgName}}.install -{{- end }} -{{- if .License}} -license={{.License}} -{{- else }} -license="CUSTOM" -{{- end }} - -options="!check !fhs" - -package() {{.Package}} -` const postInstall = ` {{- if .PreInst}} pre_install() { @@ -73,3 +41,36 @@ post_remove() { } {{- end }} ` + +const specFile = ` +{{- /* Mandatory fields */ -}} +pkgname={{.PkgName}} +{{- if .Epoch}} +epoch={{.Epoch}} +{{- end }} +pkgver={{.PkgVer}} +pkgrel={{.PkgRel}} +pkgdesc="{{.PkgDesc}}" +arch="all" +{{- with .Depends}} +depends="{{join .}}" +{{- end }} +{{- with .Conflicts}} +conflicts=({{join .}}) +{{- end }} +{{- if .URL}} +url="{{.URL}}" +{{- end }} +{{- if .Install}} +install={{.PkgName}}.install +{{- end }} +{{- if .License}} +license={{.License}} +{{- else }} +license="CUSTOM" +{{- end }} + +options="!check !fhs" + +package() {{.Package}} +` diff --git a/builder/builder.go b/builder/builder.go index aa53a9e..3efe4a9 100644 --- a/builder/builder.go +++ b/builder/builder.go @@ -13,43 +13,38 @@ type Builder struct { PKGBUILD *pkgbuild.PKGBUILD } -func (builder *Builder) initDirs() error { - err := utils.ExistsMakeDir(builder.PKGBUILD.SourceDir) +func (builder *Builder) Compile() error { + err := builder.initDirs() if err != nil { return err } - err = utils.ExistsMakeDir(builder.PKGBUILD.PackageDir) + fmt.Printf("%s🖧 :: %sGetting sources ...%s\n", + string(constants.ColorBlue), + string(constants.ColorYellow), + string(constants.ColorWhite)) + + err = builder.getSources() if err != nil { return err } - return err -} - -func (builder *Builder) getSources() error { - var err error - - for index, sourceURI := range builder.PKGBUILD.SourceURI { - source := source.Source{ - StartDir: builder.PKGBUILD.StartDir, - Hash: builder.PKGBUILD.HashSums[index], - SourceItemURI: sourceURI, - SrcDir: builder.PKGBUILD.SourceDir, - SourceItemPath: "", - } - err = source.Get() + fmt.Printf("%s🏗️ :: %sBuilding ...%s\n", + string(constants.ColorBlue), + string(constants.ColorYellow), + string(constants.ColorWhite)) - if err != nil { - return err - } + err = builder.build() + if err != nil { + return err } - return err -} + fmt.Printf("%s📦 :: %sGenerating package ...%s\n", + string(constants.ColorBlue), + string(constants.ColorYellow), + string(constants.ColorWhite)) -func (builder *Builder) build() error { - err := RunScript(builder.PKGBUILD.Build) + err = builder.Package() if err != nil { return err } @@ -66,38 +61,43 @@ func (builder *Builder) Package() error { return err } -func (builder *Builder) Compile() error { - err := builder.initDirs() +func (builder *Builder) build() error { + err := RunScript(builder.PKGBUILD.Build) if err != nil { return err } - fmt.Printf("%s🖧 :: %sGetting sources ...%s\n", - string(constants.ColorBlue), - string(constants.ColorYellow), - string(constants.ColorWhite)) + return err +} - err = builder.getSources() - if err != nil { - return err +func (builder *Builder) getSources() error { + var err error + + for index, sourceURI := range builder.PKGBUILD.SourceURI { + source := source.Source{ + StartDir: builder.PKGBUILD.StartDir, + Hash: builder.PKGBUILD.HashSums[index], + SourceItemURI: sourceURI, + SrcDir: builder.PKGBUILD.SourceDir, + SourceItemPath: "", + } + err = source.Get() + + if err != nil { + return err + } } - fmt.Printf("%s🏗️ :: %sBuilding ...%s\n", - string(constants.ColorBlue), - string(constants.ColorYellow), - string(constants.ColorWhite)) + return err +} - err = builder.build() +func (builder *Builder) initDirs() error { + err := utils.ExistsMakeDir(builder.PKGBUILD.SourceDir) if err != nil { return err } - fmt.Printf("%s📦 :: %sGenerating package ...%s\n", - string(constants.ColorBlue), - string(constants.ColorYellow), - string(constants.ColorWhite)) - - err = builder.Package() + err = utils.ExistsMakeDir(builder.PKGBUILD.PackageDir) if err != nil { return err } diff --git a/cmd/build.go b/cmd/build.go index ae0fc74..8525a70 100644 --- a/cmd/build.go +++ b/cmd/build.go @@ -13,50 +13,52 @@ import ( "github.com/spf13/cobra" ) -var NoCache bool +var ( + NoCache bool -// buildCmd represents the command to build the entire project. -var buildCmd = &cobra.Command{ - Use: "build [target] [path]", - Short: "Build multiple PKGBUILD definitions within a yap.json project", - Args: cobra.MinimumNArgs(2), - Run: func(cmd *cobra.Command, args []string) { - fullJSONPath, _ := filepath.Abs(args[1]) + // buildCmd represents the command to build the entire project. + buildCmd = &cobra.Command{ + Use: "build [target] [path]", + Short: "Build multiple PKGBUILD definitions within a yap.json project", + Args: cobra.MinimumNArgs(2), + Run: func(cmd *cobra.Command, args []string) { + fullJSONPath, _ := filepath.Abs(args[1]) - split := strings.Split(args[0], "-") - distro := split[0] - release := "" + split := strings.Split(args[0], "-") + distro := split[0] + release := "" - if len(split) > 1 { - release = split[1] - } - - mpc := project.MultipleProject{} - err := mpc.MultiProject(distro, release, fullJSONPath) - if err != nil { - fmt.Printf("%s❌ :: %serror:\n", - string(constants.ColorBlue), - string(constants.ColorYellow)) - log.Fatal(err) - } + if len(split) > 1 { + release = split[1] + } - if project.NoCache { - if err := mpc.Clean(); err != nil { + mpc := project.MultipleProject{} + err := mpc.MultiProject(distro, release, fullJSONPath) + if err != nil { fmt.Printf("%s❌ :: %serror:\n", string(constants.ColorBlue), string(constants.ColorYellow)) log.Fatal(err) } - } - if err := mpc.BuildAll(); err != nil { - fmt.Printf("%s❌ :: %serror:\n", - string(constants.ColorBlue), - string(constants.ColorYellow)) - log.Fatal(err) - } - }, -} + if project.NoCache { + if err := mpc.Clean(); err != nil { + fmt.Printf("%s❌ :: %serror:\n", + string(constants.ColorBlue), + string(constants.ColorYellow)) + log.Fatal(err) + } + } + + if err := mpc.BuildAll(); err != nil { + fmt.Printf("%s❌ :: %serror:\n", + string(constants.ColorBlue), + string(constants.ColorYellow)) + log.Fatal(err) + } + }, + } +) func init() { rootCmd.AddCommand(buildCmd) diff --git a/cmd/listTargets.go b/cmd/listTargets.go index cee81e1..4809cd0 100644 --- a/cmd/listTargets.go +++ b/cmd/listTargets.go @@ -8,12 +8,6 @@ import ( "github.com/spf13/cobra" ) -func ListTargets() { - for _, release := range constants.Releases { - fmt.Println(strings.ReplaceAll(release, "_", "-")) - } -} - // listTargetsCmd represents the listTargets command. var listTargetsCmd = &cobra.Command{ Use: "list-targets", @@ -23,6 +17,12 @@ var listTargetsCmd = &cobra.Command{ }, } +func ListTargets() { + for _, release := range constants.Releases { + fmt.Println(strings.ReplaceAll(release, "_", "-")) + } +} + func init() { rootCmd.AddCommand(listTargetsCmd) } diff --git a/cmd/prepare.go b/cmd/prepare.go index efe1da9..de6a819 100644 --- a/cmd/prepare.go +++ b/cmd/prepare.go @@ -12,42 +12,44 @@ import ( "github.com/spf13/cobra" ) -var GoLang bool - -// prepareCmd represents the listTargets command. -var prepareCmd = &cobra.Command{ - Use: "prepare [target]", - Short: "Install base development packages for every supported distro", - Args: cobra.MinimumNArgs(1), - Run: func(cmd *cobra.Command, args []string) { - split := strings.Split(args[0], "-") - distro := split[0] - - packageManager := packer.GetPackageManager(&pkgbuild.PKGBUILD{}, distro) - if !project.SkipSyncFlag { - if err := packageManager.Update(); err != nil { - log.Fatal(err) +var ( + GoLang bool + + // prepareCmd represents the listTargets command. + prepareCmd = &cobra.Command{ + Use: "prepare [target]", + Short: "Install base development packages for every supported distro", + Args: cobra.MinimumNArgs(1), + Run: func(cmd *cobra.Command, args []string) { + split := strings.Split(args[0], "-") + distro := split[0] + + packageManager := packer.GetPackageManager(&pkgbuild.PKGBUILD{}, distro) + if !project.SkipSyncFlag { + if err := packageManager.Update(); err != nil { + log.Fatal(err) + } } - } - - err := packageManager.PrepareEnvironment(GoLang) - if err != nil { - log.Fatal(err) - } - fmt.Printf("%s🪛 :: %sBasic build environment successfully prepared%s\n", - string(constants.ColorBlue), - string(constants.ColorYellow), - string(constants.ColorWhite)) + err := packageManager.PrepareEnvironment(GoLang) + if err != nil { + log.Fatal(err) + } - if GoLang { - fmt.Printf("%s🪛 :: %sGO successfully installed%s\n", + fmt.Printf("%s🪛 :: %sBasic build environment successfully prepared%s\n", string(constants.ColorBlue), string(constants.ColorYellow), string(constants.ColorWhite)) - } - }, -} + + if GoLang { + fmt.Printf("%s🪛 :: %sGO successfully installed%s\n", + string(constants.ColorBlue), + string(constants.ColorYellow), + string(constants.ColorWhite)) + } + }, + } +) func init() { rootCmd.AddCommand(prepareCmd) diff --git a/cmd/version.go b/cmd/version.go index 544fa5a..33121c4 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -6,10 +6,6 @@ import ( "github.com/spf13/cobra" ) -func init() { - rootCmd.AddCommand(versionCmd) -} - var versionCmd = &cobra.Command{ Use: "version", Short: "Print the version number of Yap", @@ -18,3 +14,7 @@ var versionCmd = &cobra.Command{ fmt.Println("Yap v1.0") }, } + +func init() { + rootCmd.AddCommand(versionCmd) +} diff --git a/constants/constants.go b/constants/constants.go index 936896a..a4c86c9 100644 --- a/constants/constants.go +++ b/constants/constants.go @@ -9,10 +9,10 @@ import ( ) const ( - DockerOrg = "m0rf30/yap-" ColorYellow = "\033[33m" ColorBlue = "\033[34m" ColorWhite = "\033[37m" + DockerOrg = "m0rf30/yap-" GoArchiveURL = "https://go.dev/dl/go1.21.3.linux-amd64.tar.gz" ) diff --git a/docker/debian-buster/Dockerfile b/docker/debian-buster/Dockerfile index ce37198..723c3ca 100644 --- a/docker/debian-buster/Dockerfile +++ b/docker/debian-buster/Dockerfile @@ -35,6 +35,7 @@ RUN \ apt-get clean; \ rm -rf /var/lib/apt/lists/* +ENV DEBIAN_FRONTEND=noninteractive ENV GOROOT=/usr/lib/go ENV PATH /usr/lib/go/bin:$PATH:/go/bin diff --git a/docker/debian-jessie/Dockerfile b/docker/debian-jessie/Dockerfile index da17cb1..de83b4d 100644 --- a/docker/debian-jessie/Dockerfile +++ b/docker/debian-jessie/Dockerfile @@ -35,6 +35,7 @@ RUN \ apt-get clean; \ rm -rf /var/lib/apt/lists/* +ENV DEBIAN_FRONTEND=noninteractive ENV GOROOT=/usr/lib/go ENV PATH /usr/lib/go/bin:$PATH:/go/bin diff --git a/docker/debian-stretch/Dockerfile b/docker/debian-stretch/Dockerfile index 04cbf81..73f7eff 100644 --- a/docker/debian-stretch/Dockerfile +++ b/docker/debian-stretch/Dockerfile @@ -35,6 +35,7 @@ RUN \ apt-get clean; \ rm -rf /var/lib/apt/lists/* +ENV DEBIAN_FRONTEND=noninteractive ENV GOROOT=/usr/lib/go ENV PATH /usr/lib/go/bin:$PATH:/go/bin diff --git a/docker/ubuntu-bionic/Dockerfile b/docker/ubuntu-bionic/Dockerfile index 4d577c0..86f803d 100644 --- a/docker/ubuntu-bionic/Dockerfile +++ b/docker/ubuntu-bionic/Dockerfile @@ -35,6 +35,7 @@ RUN \ apt-get clean; \ rm -rf /var/lib/apt/lists/* +ENV DEBIAN_FRONTEND=noninteractive ENV GOROOT=/usr/lib/go ENV PATH /usr/lib/go/bin:$PATH:/go/bin diff --git a/docker/ubuntu-focal/Dockerfile b/docker/ubuntu-focal/Dockerfile index 66c8895..522dca7 100644 --- a/docker/ubuntu-focal/Dockerfile +++ b/docker/ubuntu-focal/Dockerfile @@ -35,6 +35,7 @@ RUN \ apt-get clean; \ rm -rf /var/lib/apt/lists/* +ENV DEBIAN_FRONTEND=noninteractive ENV GOROOT=/usr/lib/go ENV PATH /usr/lib/go/bin:$PATH:/go/bin diff --git a/docker/ubuntu-jammy/Dockerfile b/docker/ubuntu-jammy/Dockerfile index 90ed526..59c524c 100644 --- a/docker/ubuntu-jammy/Dockerfile +++ b/docker/ubuntu-jammy/Dockerfile @@ -35,6 +35,7 @@ RUN \ apt-get clean; \ rm -rf /var/lib/apt/lists/* +ENV DEBIAN_FRONTEND=noninteractive ENV GOROOT=/usr/lib/go ENV PATH /usr/lib/go/bin:$PATH:/go/bin diff --git a/pacman/constants.go b/pacman/constants.go index 2be1697..68db140 100644 --- a/pacman/constants.go +++ b/pacman/constants.go @@ -4,6 +4,44 @@ var buildEnvironmentDeps = []string{ "base-devel", } +const postInstall = ` +{{- if .PreInst}} +pre_install() { + {{.PreInst}}" +} +{{- end }} + +{{- if .PostInst}} +post_install() { + {{.PostInst}}" +} +{{- end }} + +{{- if .PreInst}} +pre_upgrade() { + {{.PreInst}}" +} +{{- end }} + +{{- if .PostInst}} +post_upgrade() { + {{.PostInst}}" +} +{{- end }} + +{{- if .PreRm}} +pre_remove() { + {{.PreRm}}" +} +{{- end }} + +{{- if .PostRm}} +post_remove() { + {{.PostRm}}" +} +{{- end }} +` + const specFile = ` {{- /* Mandatory fields */ -}} # Maintainer: {{.Maintainer}} @@ -50,40 +88,3 @@ install={{.PkgName}}.install package() {{.Package}} ` -const postInstall = ` -{{- if .PreInst}} -pre_install() { - {{.PreInst}}" -} -{{- end }} - -{{- if .PostInst}} -post_install() { - {{.PostInst}}" -} -{{- end }} - -{{- if .PreInst}} -pre_upgrade() { - {{.PreInst}}" -} -{{- end }} - -{{- if .PostInst}} -post_upgrade() { - {{.PostInst}}" -} -{{- end }} - -{{- if .PreRm}} -pre_remove() { - {{.PreRm}}" -} -{{- end }} - -{{- if .PostRm}} -post_remove() { - {{.PostRm}}" -} -{{- end }} -` diff --git a/pacman/pacman.go b/pacman/pacman.go index ec5303d..802e5a3 100644 --- a/pacman/pacman.go +++ b/pacman/pacman.go @@ -12,38 +12,6 @@ type Pacman struct { pacmanDir string } -func (p *Pacman) pacmanBuild() error { - err := utils.Exec(p.pacmanDir, "makepkg", "-f") - if err != nil { - return err - } - - return err -} - -func (p *Pacman) Prepare(makeDepends []string) error { - args := []string{ - "-S", - "--noconfirm", - } - - err := p.PKGBUILD.GetDepends("pacman", args, makeDepends) - if err != nil { - return err - } - - return err -} - -func (p *Pacman) Update() error { - err := p.PKGBUILD.GetUpdates("pacman", "-Sy") - if err != nil { - return err - } - - return err -} - func (p *Pacman) Build(artifactsPath string) error { p.pacmanDir = p.PKGBUILD.StartDir @@ -91,6 +59,20 @@ func (p *Pacman) Install(artifactsPath string) error { return err } +func (p *Pacman) Prepare(makeDepends []string) error { + args := []string{ + "-S", + "--noconfirm", + } + + err := p.PKGBUILD.GetDepends("pacman", args, makeDepends) + if err != nil { + return err + } + + return err +} + func (p *Pacman) PrepareEnvironment(golang bool) error { var err error @@ -113,3 +95,21 @@ func (p *Pacman) PrepareEnvironment(golang bool) error { return err } + +func (p *Pacman) Update() error { + err := p.PKGBUILD.GetUpdates("pacman", "-Sy") + if err != nil { + return err + } + + return err +} + +func (p *Pacman) pacmanBuild() error { + err := utils.Exec(p.pacmanDir, "makepkg", "-f") + if err != nil { + return err + } + + return err +} diff --git a/parser/parser.go b/parser/parser.go index 19c1337..c3f45ad 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -14,6 +14,55 @@ import ( var OverridePkgver string +func ParseFile(distro, release, startDir, home string) (*pkgbuild.PKGBUILD, error) { + home, err := filepath.Abs(home) + + if err != nil { + fmt.Printf("%s❌ :: %sfailed to get root directory from %s\n", + string(constants.ColorBlue), + string(constants.ColorYellow), home) + + return nil, err + } + + pkgbuild := &pkgbuild.PKGBUILD{ + Distro: distro, + Codename: release, + StartDir: startDir, + Home: home, + SourceDir: filepath.Join(startDir, "src"), + PackageDir: filepath.Join(startDir, "staging"), + } + + err = utils.ExistsMakeDir(startDir) + if err != nil { + return pkgbuild, err + } + + err = copy.Copy(home, startDir) + if err != nil { + return pkgbuild, err + } + + pkgbuild.Init() + + pkgbuildSyntax, err := getSyntaxFile(startDir, home) + if err != nil { + return nil, err + } + + err = parseSyntaxFile(pkgbuildSyntax, pkgbuild) + if err != nil { + return nil, err + } + + if OverridePkgver != "" { + pkgbuild.PkgVer = OverridePkgver + } + + return pkgbuild, err +} + func getSyntaxFile(startDir, home string) (*syntax.File, error) { file, err := utils.Open(filepath.Join(startDir, "PKGBUILD")) if err != nil { @@ -98,52 +147,3 @@ func parseSyntaxFile(pkgbuildSyntax *syntax.File, pkgbuild *pkgbuild.PKGBUILD) e return err } - -func ParseFile(distro, release, startDir, home string) (*pkgbuild.PKGBUILD, error) { - home, err := filepath.Abs(home) - - if err != nil { - fmt.Printf("%s❌ :: %sfailed to get root directory from %s\n", - string(constants.ColorBlue), - string(constants.ColorYellow), home) - - return nil, err - } - - pkgbuild := &pkgbuild.PKGBUILD{ - Distro: distro, - Codename: release, - StartDir: startDir, - Home: home, - SourceDir: filepath.Join(startDir, "src"), - PackageDir: filepath.Join(startDir, "staging"), - } - - err = utils.ExistsMakeDir(startDir) - if err != nil { - return pkgbuild, err - } - - err = copy.Copy(home, startDir) - if err != nil { - return pkgbuild, err - } - - pkgbuild.Init() - - pkgbuildSyntax, err := getSyntaxFile(startDir, home) - if err != nil { - return nil, err - } - - err = parseSyntaxFile(pkgbuildSyntax, pkgbuild) - if err != nil { - return nil, err - } - - if OverridePkgver != "" { - pkgbuild.PkgVer = OverridePkgver - } - - return pkgbuild, err -} diff --git a/pkgbuild/pkgbuild.go b/pkgbuild/pkgbuild.go index 2d6cf1d..17932b7 100644 --- a/pkgbuild/pkgbuild.go +++ b/pkgbuild/pkgbuild.go @@ -60,6 +60,123 @@ type PKGBUILD struct { priorities map[string]int } +func (p *PKGBUILD) AddItem(key string, data interface{}) error { + key, priority, err := p.parseDirective(key) + if err != nil { + return err + } + + if priority == -1 { + return err + } + + if priority < p.priorities[key] { + return err + } + + p.priorities[key] = priority + + p.mapVariables(key, data) + p.mapArrays(key, data) + p.mapFunctions(key, data) + + return err +} + +func (p *PKGBUILD) CreateSpec(filePath string, script string) error { + cleanFilePath := filepath.Clean(filePath) + + file, err := os.Create(cleanFilePath) + if err != nil { + log.Panic(err) + } + + defer file.Close() + writer := io.Writer(file) + + tmpl := template.New("template") + tmpl.Funcs(template.FuncMap{ + "join": func(strs []string) string { + return strings.Trim(strings.Join(strs, ", "), " ") + }, + "multiline": func(strs string) string { + ret := strings.ReplaceAll(strs, "\n", "\n ") + + return strings.Trim(ret, " \n") + }, + }) + + template.Must(tmpl.Parse(script)) + + if Verbose { + err = tmpl.Execute(os.Stdout, p) + if err != nil { + log.Panic(err) + } + } + + err = tmpl.Execute(writer, p) + if err != nil { + log.Panic(err) + } + + return err +} + +func (p *PKGBUILD) GetDepends(packageManager string, args []string, makeDepends []string) error { + var err error + if len(makeDepends) == 0 { + return err + } + + args = append(args, makeDepends...) + + err = utils.Exec("", packageManager, args...) + if err != nil { + return err + } + + return err +} + +func (p *PKGBUILD) GetUpdates(packageManager string, args ...string) error { + err := utils.Exec("", packageManager, args...) + if err != nil { + return err + } + + return err +} + +func (p *PKGBUILD) Init() { + p.priorities = map[string]int{} + + p.FullDistroName = p.Distro + if p.Codename != "" { + p.FullDistroName += "_" + p.Codename + } +} + +func (p *PKGBUILD) Validate() { + if len(p.SourceURI) != len(p.HashSums) { + fmt.Printf("%s%s ❌ :: %snumber of sources and hashsums differs%s\n", + p.PkgName, + string(constants.ColorBlue), + string(constants.ColorYellow), + string(constants.ColorWhite)) + os.Exit(1) + } + + if len(p.Package) == 0 { + fmt.Printf("%s%s ❌ :: %smissing package() function%s\n", + p.PkgName, + string(constants.ColorBlue), + string(constants.ColorYellow), + string(constants.ColorWhite)) + os.Exit(1) + } +} + func (p *PKGBUILD) mapArrays(key string, data interface{}) { switch key { case "arch": @@ -140,15 +257,6 @@ func (p *PKGBUILD) mapVariables(key string, data interface{}) { } } -func (p *PKGBUILD) Init() { - p.priorities = map[string]int{} - - p.FullDistroName = p.Distro - if p.Codename != "" { - p.FullDistroName += "_" + p.Codename - } -} - func (p *PKGBUILD) parseDirective(input string) (string, int, error) { split := strings.Split(input, "__") key := split[0] @@ -202,111 +310,3 @@ func (p *PKGBUILD) parseDirective(input string) (string, int, error) { return key, priority, err } - -func (p *PKGBUILD) AddItem(key string, data interface{}) error { - key, priority, err := p.parseDirective(key) - if err != nil { - return err - } - - if priority == -1 { - return err - } - - if priority < p.priorities[key] { - return err - } - - p.priorities[key] = priority - - p.mapVariables(key, data) - p.mapArrays(key, data) - p.mapFunctions(key, data) - - return err -} - -func (p *PKGBUILD) Validate() { - if len(p.SourceURI) != len(p.HashSums) { - fmt.Printf("%s%s ❌ :: %snumber of sources and hashsums differs%s\n", - p.PkgName, - string(constants.ColorBlue), - string(constants.ColorYellow), - string(constants.ColorWhite)) - os.Exit(1) - } - - if len(p.Package) == 0 { - fmt.Printf("%s%s ❌ :: %smissing package() function%s\n", - p.PkgName, - string(constants.ColorBlue), - string(constants.ColorYellow), - string(constants.ColorWhite)) - os.Exit(1) - } -} - -func (p *PKGBUILD) GetDepends(packageManager string, args []string, makeDepends []string) error { - var err error - if len(makeDepends) == 0 { - return err - } - - args = append(args, makeDepends...) - - err = utils.Exec("", packageManager, args...) - if err != nil { - return err - } - - return err -} - -func (p *PKGBUILD) GetUpdates(packageManager string, args ...string) error { - err := utils.Exec("", packageManager, args...) - if err != nil { - return err - } - - return err -} - -func (p *PKGBUILD) CreateSpec(filePath string, script string) error { - cleanFilePath := filepath.Clean(filePath) - - file, err := os.Create(cleanFilePath) - if err != nil { - log.Panic(err) - } - - defer file.Close() - writer := io.Writer(file) - - tmpl := template.New("template") - tmpl.Funcs(template.FuncMap{ - "join": func(strs []string) string { - return strings.Trim(strings.Join(strs, ", "), " ") - }, - "multiline": func(strs string) string { - ret := strings.ReplaceAll(strs, "\n", "\n ") - - return strings.Trim(ret, " \n") - }, - }) - - template.Must(tmpl.Parse(script)) - - if Verbose { - err = tmpl.Execute(os.Stdout, p) - if err != nil { - log.Panic(err) - } - } - - err = tmpl.Execute(writer, p) - if err != nil { - log.Panic(err) - } - - return err -} diff --git a/project/project.go b/project/project.go index 9f3417e..d5bd4ae 100644 --- a/project/project.go +++ b/project/project.go @@ -28,19 +28,6 @@ type DistroProject interface { Prepare() error } -type Project struct { - Builder *builder.Builder - BuildRoot string - Distro string - MirrorRoot string - PackageManager packer.Packer - Path string - Release string - Root string - Name string `json:"name" validate:"required,startsnotwith=.,startsnotwith=./"` - HasToInstall bool `json:"install" validate:""` -} - type MultipleProject struct { makeDepends []string packageManager packer.Packer @@ -52,25 +39,17 @@ type MultipleProject struct { Projects []*Project `json:"projects" validate:"required,dive,required"` } -func (mpc *MultipleProject) findPackageInProjects() { - var matchFound bool - - for _, proj := range mpc.Projects { - if UntilPkgName == proj.Builder.PKGBUILD.PkgName { - matchFound = true - } - } - - if !matchFound { - fmt.Printf("%s❌ :: %sPackage not found: %s%s\n", - string(constants.ColorBlue), - string(constants.ColorYellow), - string(constants.ColorWhite), - UntilPkgName, - ) - - os.Exit(1) - } +type Project struct { + Builder *builder.Builder + BuildRoot string + Distro string + MirrorRoot string + PackageManager packer.Packer + Path string + Release string + Root string + Name string `json:"name" validate:"required,startsnotwith=.,startsnotwith=./"` + HasToInstall bool `json:"install" validate:""` } func (mpc *MultipleProject) BuildAll() error { @@ -195,6 +174,37 @@ func (mpc *MultipleProject) createPackages(proj *Project) error { return err } +func (mpc *MultipleProject) findPackageInProjects() { + var matchFound bool + + for _, proj := range mpc.Projects { + if UntilPkgName == proj.Builder.PKGBUILD.PkgName { + matchFound = true + } + } + + if !matchFound { + fmt.Printf("%s❌ :: %sPackage not found: %s%s\n", + string(constants.ColorBlue), + string(constants.ColorYellow), + string(constants.ColorWhite), + UntilPkgName, + ) + + os.Exit(1) + } +} + +func (mpc *MultipleProject) getMakeDeps() { + var makeDepends []string + + for _, child := range mpc.Projects { + makeDepends = append(makeDepends, child.Builder.PKGBUILD.MakeDepends...) + } + + mpc.makeDepends = makeDepends +} + func (mpc *MultipleProject) populateProjects(distro string, release string, path string) error { var err error @@ -224,16 +234,6 @@ func (mpc *MultipleProject) populateProjects(distro string, release string, path return err } -func (mpc *MultipleProject) getMakeDeps() { - var makeDepends []string - - for _, child := range mpc.Projects { - makeDepends = append(makeDepends, child.Builder.PKGBUILD.MakeDepends...) - } - - mpc.makeDepends = makeDepends -} - func (mpc *MultipleProject) readProject(path string) error { cleanFilePath := filepath.Clean(filepath.Join(path, "yap.json")) diff --git a/redhat/constants.go b/redhat/constants.go index 40a81e2..19974c3 100644 --- a/redhat/constants.go +++ b/redhat/constants.go @@ -21,6 +21,16 @@ var buildEnvironmentDeps = []string{ } var ( + RPMArchs = map[string]string{ + "x86_64": "x86_64", + "i686": "i686", + "aarch64": "aarch64", + "armv7h": "armv7h", + "armv6h": "armv6h", + "arm": "arm", + "any": "noarch", + } + RPMGroups = map[string]string{ "admin": "Applications/System", "any": "noarch", @@ -56,16 +66,6 @@ var ( "x11": "User Interface/X", } - RPMArchs = map[string]string{ - "x86_64": "x86_64", - "i686": "i686", - "aarch64": "aarch64", - "armv7h": "armv7h", - "armv6h": "armv6h", - "arm": "arm", - "any": "noarch", - } - RPMDistros = map[string]string{ "amazon": ".amzn", "fedora": ".fc", diff --git a/redhat/redhat.go b/redhat/redhat.go index c86eb07..04b9636 100644 --- a/redhat/redhat.go +++ b/redhat/redhat.go @@ -23,20 +23,115 @@ type Redhat struct { srpmsDir string } -func (r *Redhat) getRPMArch() { - for index, arch := range r.PKGBUILD.Arch { - r.PKGBUILD.Arch[index] = RPMArchs[arch] +func (r *Redhat) Build(artifactsPath string) error { + r.getRPMArch() + r.getRPMGroup() + r.getRPMRelease() + + r.PKGBUILD.PkgDest, _ = filepath.Abs(artifactsPath) + + err := utils.RemoveAll(r.redhatDir) + if err != nil { + return err + } + + err = r.makeDirs() + if err != nil { + return err + } + + err = r.getFiles() + if err != nil { + return err + } + + buildRootPackageDir := fmt.Sprintf("%s/%s-%s-%s.%s", + r.buildRootDir, + r.PKGBUILD.PkgName, + r.PKGBUILD.PkgVer, + r.PKGBUILD.PkgRel, + r.PKGBUILD.Arch[0]) + + err = copy.Copy(r.PKGBUILD.PackageDir, buildRootPackageDir) + if err != nil { + return err } + + err = r.PKGBUILD.CreateSpec(filepath.Join(r.specsDir, r.PKGBUILD.PkgName+".spec"), specFile) + if err != nil { + return err + } + + err = r.rpmBuild() + if err != nil { + return err + } + + return err } -func (r *Redhat) getRPMRelease() { - if r.PKGBUILD.Codename != "" { - r.PKGBUILD.PkgRel = r.PKGBUILD.PkgRel + RPMDistros[r.PKGBUILD.Distro] + r.PKGBUILD.Codename +func (r *Redhat) Install(artifactsPath string) error { + var err error + + for _, arch := range r.PKGBUILD.Arch { + pkgName := r.PKGBUILD.PkgName + "-" + + r.PKGBUILD.PkgVer + + "-" + + r.PKGBUILD.PkgRel + + "." + + RPMArchs[arch] + + ".rpm" + + pkgFilePath := filepath.Join(artifactsPath, RPMArchs[arch], pkgName) + + if err := utils.Exec("", "yum", "install", "-y", pkgFilePath); err != nil { + return err + } } + + return err } -func (r *Redhat) getRPMGroup() { - r.PKGBUILD.Section = RPMGroups[r.PKGBUILD.Section] +func (r *Redhat) PrepareEnvironment(golang bool) error { + var err error + + args := []string{ + "-y", + "install", + } + args = append(args, buildEnvironmentDeps...) + + err = utils.Exec("", "yum", args...) + + if err != nil { + return err + } + + if golang { + utils.GOSetup() + } + + return err +} + +func (r *Redhat) Prepare(makeDepends []string) error { + args := []string{ + "-y", + "install", + } + + err := r.PKGBUILD.GetDepends("dnf", args, makeDepends) + if err != nil { + return err + } + + return err +} + +func (r *Redhat) Update() error { + var err error + + return err } func (r *Redhat) getFiles() error { @@ -93,34 +188,20 @@ func (r *Redhat) getFiles() error { return err } -func (r *Redhat) rpmBuild() error { - err := utils.Exec(r.specsDir, "rpmbuild", "--define", - "_topdir "+r.redhatDir, "-bb", r.PKGBUILD.PkgName+".spec") - if err != nil { - return err +func (r *Redhat) getRPMArch() { + for index, arch := range r.PKGBUILD.Arch { + r.PKGBUILD.Arch[index] = RPMArchs[arch] } - - return err } -func (r *Redhat) Prepare(makeDepends []string) error { - args := []string{ - "-y", - "install", - } - - err := r.PKGBUILD.GetDepends("dnf", args, makeDepends) - if err != nil { - return err - } - - return err +func (r *Redhat) getRPMGroup() { + r.PKGBUILD.Section = RPMGroups[r.PKGBUILD.Section] } -func (r *Redhat) Update() error { - var err error - - return err +func (r *Redhat) getRPMRelease() { + if r.PKGBUILD.Codename != "" { + r.PKGBUILD.PkgRel = r.PKGBUILD.PkgRel + RPMDistros[r.PKGBUILD.Distro] + r.PKGBUILD.Codename + } } func (r *Redhat) makeDirs() error { @@ -152,93 +233,12 @@ func (r *Redhat) makeDirs() error { return err } -func (r *Redhat) Build(artifactsPath string) error { - r.getRPMArch() - r.getRPMGroup() - r.getRPMRelease() - - r.PKGBUILD.PkgDest, _ = filepath.Abs(artifactsPath) - - err := utils.RemoveAll(r.redhatDir) - if err != nil { - return err - } - - err = r.makeDirs() - if err != nil { - return err - } - - err = r.getFiles() - if err != nil { - return err - } - - buildRootPackageDir := fmt.Sprintf("%s/%s-%s-%s.%s", - r.buildRootDir, - r.PKGBUILD.PkgName, - r.PKGBUILD.PkgVer, - r.PKGBUILD.PkgRel, - r.PKGBUILD.Arch[0]) - - err = copy.Copy(r.PKGBUILD.PackageDir, buildRootPackageDir) - if err != nil { - return err - } - - err = r.PKGBUILD.CreateSpec(filepath.Join(r.specsDir, r.PKGBUILD.PkgName+".spec"), specFile) - if err != nil { - return err - } - - err = r.rpmBuild() - if err != nil { - return err - } - - return err -} - -func (r *Redhat) Install(artifactsPath string) error { - var err error - - for _, arch := range r.PKGBUILD.Arch { - pkgName := r.PKGBUILD.PkgName + "-" + - r.PKGBUILD.PkgVer + - "-" + - r.PKGBUILD.PkgRel + - "." + - RPMArchs[arch] + - ".rpm" - - pkgFilePath := filepath.Join(artifactsPath, RPMArchs[arch], pkgName) - - if err := utils.Exec("", "yum", "install", "-y", pkgFilePath); err != nil { - return err - } - } - - return err -} - -func (r *Redhat) PrepareEnvironment(golang bool) error { - var err error - - args := []string{ - "-y", - "install", - } - args = append(args, buildEnvironmentDeps...) - - err = utils.Exec("", "yum", args...) - +func (r *Redhat) rpmBuild() error { + err := utils.Exec(r.specsDir, "rpmbuild", "--define", + "_topdir "+r.redhatDir, "-bb", r.PKGBUILD.PkgName+".spec") if err != nil { return err } - if golang { - utils.GOSetup() - } - return err } diff --git a/set/set.go b/set/set.go index c4b66f7..36c43d6 100644 --- a/set/set.go +++ b/set/set.go @@ -6,22 +6,10 @@ type Set struct { m map[string]struct{} } -func NewSet() *Set { - s := &Set{ - m: make(map[string]struct{}), - } - - return s -} - func (s *Set) Add(value string) { s.m[value] = exists } -func (s *Set) Remove(value string) { - delete(s.m, value) -} - func (s *Set) Contains(value string) bool { _, c := s.m[value] @@ -41,3 +29,15 @@ func (s *Set) Iter() <-chan interface{} { return iter } + +func NewSet() *Set { + s := &Set{ + m: make(map[string]struct{}), + } + + return s +} + +func (s *Set) Remove(value string) { + delete(s.m, value) +} diff --git a/source/source.go b/source/source.go index 1740428..45e2ab3 100644 --- a/source/source.go +++ b/source/source.go @@ -31,6 +31,75 @@ type Source struct { StartDir string } +func (src *Source) Get() error { + var err error + + src.parseURI() + + sourceType := src.getType() + + switch sourceType { + case "http": + src.getURL("http") + case "https": + src.getURL("https") + case "ftp": + src.getURL("ftp") + case "git": + src.getURL("git") + case "file": + default: + fmt.Printf("%s❌ :: %sunknown or unsupported source type: %s\n", + string(constants.ColorBlue), + string(constants.ColorYellow), sourceType) + + os.Exit(1) + } + + if err != nil { + return err + } + + err = src.validate() + if err != nil { + return err + } + + err = src.symlinkSources() + if err != nil { + return err + } + + err = src.extract() + if err != nil { + return err + } + + return err +} + +func (src *Source) extract() error { + dlFile, err := os.Open(filepath.Join(src.StartDir, src.SourceItemPath)) + if err != nil { + fmt.Printf("%s❌ :: %sfailed to open source %s\n", + string(constants.ColorBlue), + string(constants.ColorYellow), src.SourceItemURI) + + return err + } + + err = utils.Unarchive(dlFile, src.SrcDir) + if err != nil { + fmt.Printf("%s❌ :: %sfailed to extract source %s\n", + string(constants.ColorBlue), + string(constants.ColorYellow), src.SourceItemPath) + + log.Panic(err) + } + + return err +} + func (src *Source) getReferenceType() plumbing.ReferenceName { var referenceName plumbing.ReferenceName @@ -65,25 +134,6 @@ func (src *Source) getType() string { return "file" } -func (src *Source) parseURI() { - src.SourceItemPath = utils.Filename(src.SourceItemURI) - - if strings.Contains(src.SourceItemURI, "::") { - split := strings.Split(src.SourceItemURI, "::") - src.SourceItemPath = split[0] - src.SourceItemURI = split[1] - } - - if strings.Contains(src.SourceItemURI, "#") { - split := strings.Split(src.SourceItemURI, "#") - src.SourceItemURI = split[0] - fragment := split[1] - splitFragment := strings.Split(fragment, "=") - src.RefKey = splitFragment[0] - src.RefValue = splitFragment[1] - } -} - func (src *Source) getURL(protocol string) { dloadFilePath := filepath.Join(src.StartDir, src.SourceItemPath) @@ -108,23 +158,34 @@ func (src *Source) getURL(protocol string) { } } -func (src *Source) extract() error { - dlFile, err := os.Open(filepath.Join(src.StartDir, src.SourceItemPath)) - if err != nil { - fmt.Printf("%s❌ :: %sfailed to open source %s\n", - string(constants.ColorBlue), - string(constants.ColorYellow), src.SourceItemURI) +func (src *Source) parseURI() { + src.SourceItemPath = utils.Filename(src.SourceItemURI) - return err + if strings.Contains(src.SourceItemURI, "::") { + split := strings.Split(src.SourceItemURI, "::") + src.SourceItemPath = split[0] + src.SourceItemURI = split[1] } - err = utils.Unarchive(dlFile, src.SrcDir) - if err != nil { - fmt.Printf("%s❌ :: %sfailed to extract source %s\n", - string(constants.ColorBlue), - string(constants.ColorYellow), src.SourceItemPath) + if strings.Contains(src.SourceItemURI, "#") { + split := strings.Split(src.SourceItemURI, "#") + src.SourceItemURI = split[0] + fragment := split[1] + splitFragment := strings.Split(fragment, "=") + src.RefKey = splitFragment[0] + src.RefValue = splitFragment[1] + } +} - log.Panic(err) +func (src *Source) symlinkSources() error { + var err error + + symlinkSource := filepath.Join(src.StartDir, src.SourceItemPath) + + symLinkTarget := filepath.Join(src.SrcDir, src.SourceItemPath) + + if !utils.Exists(symLinkTarget) { + err = os.Symlink(symlinkSource, symLinkTarget) } return err @@ -173,68 +234,7 @@ func (src *Source) validate() error { if hexSum != src.Hash { fmt.Printf("%s❌ :: %sHash verification failed for %s\n", string(constants.ColorBlue), - string(constants.ColorYellow), src.SourceItemURI) - } - - return err -} - -func (src *Source) Get() error { - var err error - - src.parseURI() - - sourceType := src.getType() - - switch sourceType { - case "http": - src.getURL("http") - case "https": - src.getURL("https") - case "ftp": - src.getURL("ftp") - case "git": - src.getURL("git") - case "file": - default: - fmt.Printf("%s❌ :: %sunknown or unsupported source type: %s\n", - string(constants.ColorBlue), - string(constants.ColorYellow), sourceType) - - os.Exit(1) - } - - if err != nil { - return err - } - - err = src.validate() - if err != nil { - return err - } - - err = src.symlinkSources() - if err != nil { - return err - } - - err = src.extract() - if err != nil { - return err - } - - return err -} - -func (src *Source) symlinkSources() error { - var err error - - symlinkSource := filepath.Join(src.StartDir, src.SourceItemPath) - - symLinkTarget := filepath.Join(src.SrcDir, src.SourceItemPath) - - if !utils.Exists(symLinkTarget) { - err = os.Symlink(symlinkSource, symLinkTarget) + string(constants.ColorYellow), src.SourceItemPath) } return err diff --git a/utils/file.go b/utils/file.go index fffd090..04d5f38 100644 --- a/utils/file.go +++ b/utils/file.go @@ -9,11 +9,10 @@ import ( "github.com/M0Rf30/yap/constants" ) -func MkdirAll(path string) error { - //#nosec - err := os.MkdirAll(path, 0o755) +func Chmod(path string, perm os.FileMode) error { + err := os.Chmod(path, perm) if err != nil { - fmt.Printf("%s❌ :: %sfailed to mkdir '%s'%s\n", + fmt.Printf("%s❌ :: %sfailed to chmod '%s'%s\n", string(constants.ColorBlue), string(constants.ColorYellow), path, @@ -25,40 +24,31 @@ func MkdirAll(path string) error { return err } -func Chmod(path string, perm os.FileMode) error { - err := os.Chmod(path, perm) +func Create(path string) (*os.File, error) { + cleanFilePath := filepath.Clean(path) + + file, err := os.Create(cleanFilePath) if err != nil { - fmt.Printf("%s❌ :: %sfailed to chmod '%s'%s\n", + fmt.Printf("%s❌ :: %sfailed to create '%s'%s\n", string(constants.ColorBlue), string(constants.ColorYellow), path, string(constants.ColorWhite)) - - return err } - return err + return file, err } -func Remove(path string) error { - err := os.Remove(path) +func CreateWrite(path string, data string) error { + file, err := Create(path) if err != nil { - fmt.Printf("%s❌ :: %sfailed to remove '%s'%s\n", - string(constants.ColorBlue), - string(constants.ColorYellow), - path, - string(constants.ColorWhite)) - return err } + defer file.Close() - return err -} - -func RemoveAll(path string) error { - err := os.RemoveAll(path) + _, err = file.WriteString(data) if err != nil { - fmt.Printf("%s❌ :: %sfailed to remove '%s'%s\n", + fmt.Printf("%s❌ :: %sfailed to write to file '%s'%s\n", string(constants.ColorBlue), string(constants.ColorYellow), path, @@ -70,6 +60,12 @@ func RemoveAll(path string) error { return err } +func Exists(filename string) bool { + _, err := os.Stat(filename) + + return !os.IsNotExist(err) +} + func ExistsMakeDir(path string) error { _, err := os.Stat(path) if err != nil { @@ -94,31 +90,45 @@ func ExistsMakeDir(path string) error { return err } -func Create(path string) (*os.File, error) { - cleanFilePath := filepath.Clean(path) - - file, err := os.Create(cleanFilePath) - if err != nil { - fmt.Printf("%s❌ :: %sfailed to create '%s'%s\n", - string(constants.ColorBlue), - string(constants.ColorYellow), - path, - string(constants.ColorWhite)) +func Filename(path string) string { + n := strings.LastIndex(path, "/") + if n == -1 { + return path } - return file, err + return path[n+1:] } -func CreateWrite(path string, data string) error { - file, err := Create(path) - if err != nil { +func GetDirSize(path string) (int64, error) { + var size int64 + + err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error { + if err != nil { + fmt.Printf("%s❌ :: %sfailed to get dir size '%s'%s\n", + string(constants.ColorBlue), + string(constants.ColorYellow), + path, + string(constants.ColorWhite)) + + os.Exit(1) + } + if !info.IsDir() { + size += info.Size() + } + return err - } - defer file.Close() + }) - _, err = file.WriteString(data) + size /= 1024 + + return size, err +} + +func MkdirAll(path string) error { + //#nosec + err := os.MkdirAll(path, 0o755) if err != nil { - fmt.Printf("%s❌ :: %sfailed to write to file '%s'%s\n", + fmt.Printf("%s❌ :: %sfailed to mkdir '%s'%s\n", string(constants.ColorBlue), string(constants.ColorYellow), path, @@ -145,42 +155,32 @@ func Open(path string) (*os.File, error) { return file, err } -func Filename(path string) string { - n := strings.LastIndex(path, "/") - if n == -1 { - return path +func Remove(path string) error { + err := os.Remove(path) + if err != nil { + fmt.Printf("%s❌ :: %sfailed to remove '%s'%s\n", + string(constants.ColorBlue), + string(constants.ColorYellow), + path, + string(constants.ColorWhite)) + + return err } - return path[n+1:] + return err } -func GetDirSize(path string) (int64, error) { - var size int64 - - err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error { - if err != nil { - fmt.Printf("%s❌ :: %sfailed to get dir size '%s'%s\n", - string(constants.ColorBlue), - string(constants.ColorYellow), - path, - string(constants.ColorWhite)) - - os.Exit(1) - } - if !info.IsDir() { - size += info.Size() - } +func RemoveAll(path string) error { + err := os.RemoveAll(path) + if err != nil { + fmt.Printf("%s❌ :: %sfailed to remove '%s'%s\n", + string(constants.ColorBlue), + string(constants.ColorYellow), + path, + string(constants.ColorWhite)) return err - }) - - size /= 1024 - - return size, err -} - -func Exists(filename string) bool { - _, err := os.Stat(filename) + } - return !os.IsNotExist(err) + return err } diff --git a/utils/utils.go b/utils/utils.go index 6d460eb..ddd2be0 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -30,43 +30,6 @@ func CheckGO() bool { return false } -func GOSetup() { - if CheckGO() { - return - } - - Download(goArchivePath, constants.GoArchiveURL) - - dlFile, err := os.Open(goArchivePath) - if err != nil { - fmt.Printf("%s❌ :: %sfailed to open %s\n", - string(constants.ColorBlue), - string(constants.ColorYellow), goArchivePath) - - os.Exit(1) - } - - err = Unarchive(dlFile, "/usr/lib") - if err != nil { - log.Panic(err) - } - - err = os.Symlink("/usr/lib/go/bin/go", goExecutable) - if err != nil { - log.Panic(err) - } - - err = os.Symlink("/usr/lib/go/bin/gofmt", "/usr/bin/gofmt") - if err != nil { - log.Panic(err) - } - - fmt.Printf("%s🪛 :: %sGO successfully installed%s\n", - string(constants.ColorBlue), - string(constants.ColorYellow), - string(constants.ColorWhite)) -} - func Download(destination string, url string) { // create client client := grab.NewClient() @@ -135,6 +98,43 @@ Loop: ) } +func GOSetup() { + if CheckGO() { + return + } + + Download(goArchivePath, constants.GoArchiveURL) + + dlFile, err := os.Open(goArchivePath) + if err != nil { + fmt.Printf("%s❌ :: %sfailed to open %s\n", + string(constants.ColorBlue), + string(constants.ColorYellow), goArchivePath) + + os.Exit(1) + } + + err = Unarchive(dlFile, "/usr/lib") + if err != nil { + log.Panic(err) + } + + err = os.Symlink("/usr/lib/go/bin/go", goExecutable) + if err != nil { + log.Panic(err) + } + + err = os.Symlink("/usr/lib/go/bin/gofmt", "/usr/bin/gofmt") + if err != nil { + log.Panic(err) + } + + fmt.Printf("%s🪛 :: %sGO successfully installed%s\n", + string(constants.ColorBlue), + string(constants.ColorYellow), + string(constants.ColorWhite)) +} + func PullContainers(target string) error { containerApp := "/usr/bin/docker" args := []string{