Skip to content

Commit

Permalink
feature: load packages.yaml file
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Kloubert committed May 8, 2024
1 parent a3147f0 commit 12eaa43
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 17 deletions.
6 changes: 4 additions & 2 deletions commands/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ import (
"os"
"os/exec"

"github.com/mkloubert/go-package-manager/utils"
"github.com/spf13/cobra"

"github.com/mkloubert/go-package-manager/types"
"github.com/mkloubert/go-package-manager/utils"
)

func Init_Install_Command(parentCmd *cobra.Command) {
func Init_Install_Command(parentCmd *cobra.Command, app *types.AppContext) {
var update bool

var installCmd = &cobra.Command{
Expand Down
8 changes: 5 additions & 3 deletions commands/tidy.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ import (
"os/exec"

"github.com/spf13/cobra"

"github.com/mkloubert/go-package-manager/types"
)

func Init_Tidy_Command(parentCmd *cobra.Command) {
func Init_Tidy_Command(parentCmd *cobra.Command, app *types.AppContext) {
var tidyCmd = &cobra.Command{
Use: "tidy",
Aliases: []string{"t"},
Short: "Installs one or more modules",
Long: `Gets and installs one or more modules by a short name or an URL to a git repository.`,
Short: "Add missing and remove unused modules",
Long: `Cleans up the project from unused modules and add missing ones depending on the current source code.`,
Run: func(cmd *cobra.Command, args []string) {
p := exec.Command("go", "mod", "tidy")

Expand Down
6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ go 1.22.0
require github.com/spf13/cobra v1.8.0

require (
github.com/fatih/color v1.16.0 // indirect
github.com/goccy/go-yaml v1.11.3 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect
)
15 changes: 15 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/goccy/go-yaml v1.11.3 h1:B3W9IdWbvrUu2OYQGwvU1nZtvMQJPBKgBUuweJjLj6I=
github.com/goccy/go-yaml v1.11.3/go.mod h1:wKnAMd44+9JAAnGQpWVEgBzGt3YuTaQ4uXoHvE4m7WU=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 h1:+cNy6SZtPcJQH3LJVLOSmiC7MMxXNOb3PU/VUEz+EhU=
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028/go.mod h1:NDW/Ps6MPRej6fsCIbMTohpP40sJ/P/vI1MoTEGwX90=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
32 changes: 20 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,40 @@ package main

import (
"fmt"
"os"

"github.com/mkloubert/go-package-manager/commands"
"github.com/spf13/cobra"

"github.com/mkloubert/go-package-manager/commands"
"github.com/mkloubert/go-package-manager/types"
"github.com/mkloubert/go-package-manager/utils"
)

var rootCmd = &cobra.Command{
Use: "gpm",
Short: "A package manager for Go",
Long: `A package manager for Go projects which simplifies the way of installing dependencies.`,
Use: "gpm",
Short: "Package manager for Go",
Long: `A package manager for Go projects which simplifies the way of installing dependencies and setting up projects.`,
Version: AppVersion,
Run: func(cmd *cobra.Command, args []string) {
// TODO
cmd.Help()
},
}

func main() {
var verbose bool
var app types.AppContext

// use "verbose flag" everywhere
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose output")
rootCmd.PersistentFlags().BoolVarP(&app.Verbose, "verbose", "v", false, "verbose output")

types.LoadPackagesFileIfExist(&app)

fmt.Println(app.PackagesFile)

commands.Init_Install_Command(rootCmd)
commands.Init_Tidy_Command(rootCmd)
// initialize commands
commands.Init_Install_Command(rootCmd, &app)
commands.Init_Tidy_Command(rootCmd, &app)

// execute
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
utils.CloseWithError(err)
}
}
29 changes: 29 additions & 0 deletions types/app_context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// MIT License
//
// Copyright (c) 2024 Marcel Joachim Kloubert (https://marcel.coffee)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package types

// An AppContext contains all information for running this app
type AppContext struct {
PackagesFile PackagesFile // the packages.y(a)ml file
Verbose bool // output verbose information
}
78 changes: 78 additions & 0 deletions types/packages_file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// MIT License
//
// Copyright (c) 2024 Marcel Joachim Kloubert (https://marcel.coffee)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package types

import (
"os"
"path"

"github.com/goccy/go-yaml"
"github.com/mkloubert/go-package-manager/utils"
)

// A PackagesFile stores all data of a packages.y(a)ml file.
type PackagesFile struct {
Packages map[string]PackagesFilePackageItem `yaml:"packages"` // the package mappings
}

// A PackagesFilePackageItem is an item inside `PackagesFile.Packages` map.
type PackagesFilePackageItem struct {
Sources []string `yaml:"sources"` // one or more source repositories
}

// LoadPackagesFileIfExist - Loads a packages.y(a)ml file if it exists
// and return `true` if file has been loaded successfully.
func LoadPackagesFileIfExist(app *AppContext) bool {
cwd, err := os.Getwd()
if err == nil {
packagesFilePath := path.Join(cwd, "packages.yaml")
info, err := os.Stat(packagesFilePath)
if err != nil {
if os.IsNotExist(err) {
return false
}

utils.CloseWithError(err)
}

if info.IsDir() {
return false
}

yamlData, err := os.ReadFile(packagesFilePath)
if err != nil {
utils.CloseWithError(err)
}

var pf PackagesFile
err = yaml.Unmarshal(yamlData, &pf)
if err != nil {
utils.CloseWithError(err)
}

app.PackagesFile = pf
return true
}

return false
}
7 changes: 7 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ package utils
import (
"fmt"
"net/url"
"os"
"strings"

"github.com/spf13/cobra"
Expand All @@ -46,6 +47,12 @@ func CleanupModuleName(moduleName string) string {
return strings.TrimSpace(moduleName)
}

// CloseWithError() - exits with code 1 and output an error
func CloseWithError(err error) {
fmt.Println(err)
os.Exit(1)
}

// GetBoolFlag() - returns a boolean command line flag value without error
func GetBoolFlag(cmd *cobra.Command, name string, defaultValue bool) bool {
val, err := cmd.Flags().GetBool(name)
Expand Down

0 comments on commit 12eaa43

Please sign in to comment.