Skip to content

Commit

Permalink
Merge pull request #9 from mcred/feature/distinct-add-install-commands
Browse files Browse the repository at this point in the history
Feature/distinct add install commands
  • Loading branch information
mcred authored Jul 22, 2021
2 parents 2e7912a + 5b194ac commit 60d4eed
Show file tree
Hide file tree
Showing 17 changed files with 394 additions and 186 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
bin
.idea
temp_packages.json
temp_packages.json
liquibase_modules
liquibase.json
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.2
0.0.3
14 changes: 7 additions & 7 deletions cmd/lpm/darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (

func main() {

var liquibasepath string
var liquibasehome string

if _, ok := os.LookupEnv("LIQUIBASE_HOME"); ok {
liquibasepath = os.Getenv("LIQUIBASE_HOME")
liquibasehome = os.Getenv("LIQUIBASE_HOME")
} else {
// Find Liquibase Command
out, err := exec.Command("which", "liquibase").CombinedOutput()
Expand All @@ -37,15 +37,15 @@ func main() {
log.Fatal(err)
}
// Is Symlink
liquibasepath, _ = filepath.Split(link)
liquibasehome, _ = filepath.Split(link)
} else {
// Not Symlink
liquibasepath, _ = filepath.Split(loc)
liquibasehome, _ = filepath.Split(loc)
}
}

if !strings.HasSuffix(liquibasepath, "/") {
liquibasepath = liquibasepath + "/"
if !strings.HasSuffix(liquibasehome, "/") {
liquibasehome = liquibasehome + "/"
}
commands.Execute(liquibasepath + "lib/")
commands.Execute(liquibasehome)
}
14 changes: 7 additions & 7 deletions cmd/lpm/windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (

func main() {

var liquibasepath string
var liquibasehome string

if _, ok := os.LookupEnv("LIQUIBASE_HOME"); ok {
liquibasepath = os.Getenv("LIQUIBASE_HOME")
liquibasehome = os.Getenv("LIQUIBASE_HOME")
} else {
// Find Liquibase Command
out, err := exec.Command("where", "liquibase").CombinedOutput()
Expand All @@ -37,14 +37,14 @@ func main() {
log.Fatal(err)
}
// Is Symlink
liquibasepath, _ = filepath.Split(link)
liquibasehome, _ = filepath.Split(link)
} else {
// Not Symlink
liquibasepath, _ = filepath.Split(loc)
liquibasehome, _ = filepath.Split(loc)
}
}
if !strings.HasSuffix(liquibasepath, "\\") {
liquibasepath = liquibasepath + "\\"
if !strings.HasSuffix(liquibasehome, "\\") {
liquibasehome = liquibasehome + "\\"
}
commands.Execute(liquibasepath + "lib\\")
commands.Execute(liquibasehome) //TODO confirm path separator
}
24 changes: 24 additions & 0 deletions internal/app/App.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package app
import (
_ "embed"
"encoding/json"
"io/fs"
"io/ioutil"
"os"
"package-manager/internal/app/errors"
Expand All @@ -16,11 +17,34 @@ var version string
var PackagesJSON []byte

var PackageFile = "packages.json"
var Classpath string
var ClasspathFiles []fs.FileInfo

func Version() string {
return version
}

func SetClasspath(global bool, globalpath string, globalpathFiles []fs.FileInfo) {
if global {
Classpath = globalpath
ClasspathFiles = globalpathFiles
} else {
pwd, err := os.Getwd()
if err != nil {
errors.Exit(err.Error(), 1)
}
Classpath = pwd + "/liquibase_modules/"
os.Mkdir(Classpath, 0775)
if err != nil {
errors.Exit(err.Error(), 1)
}
ClasspathFiles, err = ioutil.ReadDir(Classpath)
if err != nil {
errors.Exit(err.Error(), 1)
}
}
}

func PackagesInClassPath(cp string) bool {
_, err := os.Stat(cp + PackageFile)
return err == nil
Expand Down
2 changes: 1 addition & 1 deletion internal/app/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.2
0.0.3
73 changes: 73 additions & 0 deletions internal/app/commands/add.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package commands

import (
"fmt"
"github.com/spf13/cobra"
"package-manager/internal/app"
"package-manager/internal/app/dependencies"
"package-manager/internal/app/errors"
"package-manager/internal/app/packages"
"strings"
)

// addCmd represents the add command
var addCmd = &cobra.Command{
Use: "add [PACKAGE]...",
Short: "Add Packages",
Args: cobra.ArbitraryArgs,
Run: func(cmd *cobra.Command, args []string) {

// Set global vs local classpath
app.SetClasspath(global, globalpath, globalpathFiles)

d := dependencies.Dependencies{}

for _, name := range args {
var p packages.Package
var v packages.Version
if strings.Contains(name, "@") {
p = packs.GetByName(strings.Split(name, "@")[0])
v = p.GetVersion(strings.Split(name, "@")[1])
if v.Tag == "" {
errors.Exit("Version '"+strings.Split(name, "@")[1]+"' not available.", 1)
}
} else {
p = packs.GetByName(name)
v = p.GetLatestVersion()
}
if p.Name == "" {
errors.Exit("Package '"+name+"' not found.", 1)
}
if v.InClassPath(app.ClasspathFiles) {
errors.Exit(name+" is already installed.", 1)
}
if !v.PathIsHttp() {
v.CopyToClassPath(app.Classpath)
} else {
v.DownloadToClassPath(app.Classpath)
}
fmt.Println(v.GetFilename() + " successfully installed in classpath.")
d.Dependencies = append(d.Dependencies, dependencies.Dependency{p.Name: v.Tag})
}

if !global {
//Add package to local manifest
if !d.FileExists() {
d.CreateFile()
}
d.Write()

// Output helper for JAVA_OPTS
p := "-cp liquibase_modules/*:" + globalpath + "*:" + liquibaseHome + "liquibase.jar"
fmt.Println()
fmt.Println("---------- IMPORTANT ----------")
fmt.Println("Add the following JAVA_OPTS to your CLI:")
fmt.Println("export JAVA_OPTS=\"" + p + "\"")
}
},
}

func init() {
rootCmd.AddCommand(addCmd)
addCmd.Flags().BoolVarP(&global, "global", "g", false, "add package globally")
}
52 changes: 22 additions & 30 deletions internal/app/commands/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,38 @@ package commands
import (
"fmt"
"github.com/spf13/cobra"
"package-manager/internal/app"
"package-manager/internal/app/dependencies"
"package-manager/internal/app/errors"
"package-manager/internal/app/packages"
"strings"
)

// installCmd represents the install command
var installCmd = &cobra.Command{
Use: "install [PACKAGE]",
Short: "Install Packages",
Args: cobra.ExactArgs(1),
Use: "install",
Short: "Install Packages from liquibase.json",
Run: func(cmd *cobra.Command, args []string) {
name := args[0]
var p packages.Package
var v packages.Version
if strings.Contains(name, "@") {
p = packs.GetByName(strings.Split(name, "@")[0])
v = p.GetVersion(strings.Split(name, "@")[1])
if v.Tag == "" {
errors.Exit("Version '" + strings.Split(name, "@")[1] + "' not available.", 1)
// Set global vs local classpath
app.SetClasspath(global, globalpath, globalpathFiles)
d := dependencies.Dependencies{}
d.Read()

for _, dep := range d.Dependencies {
p := packs.GetByName(dep.GetName())
v := p.GetVersion(dep.GetVersion())

if v.InClassPath(app.ClasspathFiles) {
errors.Exit(p.Name+" is already installed.", 1)
}
} else {
p = packs.GetByName(name)
v = p.GetLatestVersion()
}
if p.Name == "" {
errors.Exit("Package '" + name + "' not found.", 1)
}
if v.InClassPath(classpathFiles) {
errors.Exit(name + " is already installed.", 1)
}
if !v.PathIsHttp() {
v.CopyToClassPath(classpath)
} else {
v.DownloadToClassPath(classpath)
if !v.PathIsHttp() {
v.CopyToClassPath(app.Classpath)
} else {
v.DownloadToClassPath(app.Classpath)
}
fmt.Println(v.GetFilename() + " successfully installed in classpath.")
}

fmt.Println(v.GetFilename() + " successfully installed in classpath.")
},
}

func init() {
rootCmd.AddCommand(installCmd)
}
}
18 changes: 12 additions & 6 deletions internal/app/commands/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,39 @@ import (
"fmt"
"github.com/spf13/cobra"
"os"
"package-manager/internal/app"
"package-manager/internal/app/packages"
)

// listCmd represents the list command
var listCmd = &cobra.Command{
Use: "ls",
Use: "list",
Short: "List Installed Packages",

Aliases: []string{"ls"},
Run: func(cmd *cobra.Command, args []string) {

// Set global vs local classpath
app.SetClasspath(global, globalpath, globalpathFiles)

// Collect installed packages
var installed packages.Packages
for _, e := range packs {
v := e.GetInstalledVersion(classpathFiles)
if v.InClassPath(classpathFiles) {
v := e.GetInstalledVersion(app.ClasspathFiles)
if v.InClassPath(app.ClasspathFiles) {
installed = append(installed, e)
}
}

// Format output
fmt.Println(classpath)
fmt.Println(app.Classpath)
if len(installed) == 0 {
os.Exit(1)
}
installed.Display(classpathFiles)
installed.Display(app.ClasspathFiles)
},
}

func init() {
rootCmd.AddCommand(listCmd)
listCmd.Flags().BoolVarP(&global, "global", "g", false, "list global packages")
}
56 changes: 56 additions & 0 deletions internal/app/commands/remove.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package commands

import (
"fmt"
"github.com/spf13/cobra"
"os"
"package-manager/internal/app"
"package-manager/internal/app/dependencies"
"package-manager/internal/app/errors"
)

// removeCmd represents the install command
var removeCmd = &cobra.Command{
Use: "remove [PACKAGE]...",
Short: "Removes Package",
Aliases: []string{"rm"},
Args: cobra.ArbitraryArgs,
Run: func(cmd *cobra.Command, args []string) {

// Set global vs local classpath
app.SetClasspath(global, globalpath, globalpathFiles)

d := dependencies.Dependencies{}
if !global {
d.Read()
}

// Remove Each Package
for _, name := range args {
p := packs.GetByName(name)
v := p.GetInstalledVersion(app.ClasspathFiles)
if p.Name == "" {
errors.Exit("Package '" + name + "' not found.", 1)
}
if !v.InClassPath(app.ClasspathFiles) {
errors.Exit(name + " is not installed.", 1)
}
err := os.Remove(app.Classpath + v.GetFilename())
if err != nil {
errors.Exit("Unable to delete " + v.GetFilename() + " from classpath.", 1)
}
fmt.Println(v.GetFilename() + " successfully uninstalled from classpath.")
if !global{
d.Remove(p.Name)
}
}
if !global{
d.Write()
}
},
}

func init() {
rootCmd.AddCommand(removeCmd)
removeCmd.Flags().BoolVarP(&global, "global", "g", false, "remove package globally")
}
Loading

0 comments on commit 60d4eed

Please sign in to comment.