-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
98 lines (80 loc) · 2.43 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"encoding/json"
"fmt"
"os"
"github.com/khanakia/pcu/composer"
"github.com/khanakia/pcu/jsonupdate"
"github.com/khanakia/pcu/packagist"
"github.com/khanakia/pcu/util"
"github.com/spf13/cobra"
)
var version = "dev"
var (
update bool
)
func main() {
rootCmd.AddCommand(checkCmd)
checkCmd.PersistentFlags().StringP("file", "f", "./composer.json", "default to current working directory")
checkCmd.PersistentFlags().StringP("out", "o", "./composer.json", "output file path")
checkCmd.PersistentFlags().BoolVarP(&update, "update", "u", false, "udpate file")
if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
var rootCmd = &cobra.Command{
Use: "pcu",
Version: version,
Short: "php-composer-update updates your package.json dependencies to their latest versions, disregarding any specified version.",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Run command `pcu --help` for more information`")
},
}
var checkCmd = &cobra.Command{
Use: "check",
Short: "Check dependencies version",
Run: func(cmd *cobra.Command, args []string) {
filepath, _ := cmd.Flags().GetString("file")
out, _ := cmd.Flags().GetString("out")
update, _ := cmd.Flags().GetBool("update")
composerCheck(filepath, out, update)
},
}
func composerCheck(filepath string, out string, update bool) {
fileBytes, err := composer.OpenFile(filepath)
if err != nil {
fmt.Println(err.Error())
return
}
jsonupd := jsonupdate.NewJsonUpdate(string(fileBytes))
var composerFile composer.ComposerFile
err = json.Unmarshal(fileBytes, &composerFile)
if err != nil {
fmt.Println("unable to parse comopser.json file")
}
for key, v := range composerFile.Require {
if packagist.ShouldSkip(key, v) {
continue
}
// fmt.Println(key)
versionName := packagist.FetchAndGetLastesVersionName(key)
newVersionName := util.TagNameConvert(versionName)
fmt.Printf("%-50.50s %-15s %s\n", key, v, newVersionName)
jsonupd.Set("require."+key, newVersionName)
}
for key, v := range composerFile.RequireDev {
if packagist.ShouldSkip(key, v) {
continue
}
// fmt.Println(key)
versionName := packagist.FetchAndGetLastesVersionName(key)
newVersionName := util.TagNameConvert(versionName)
fmt.Printf("%-50.50s %-15s %s\n", key, v, newVersionName)
jsonupd.Set("require-dev."+key, newVersionName)
}
if update {
_ = os.WriteFile(out, []byte(jsonupd.String()), 0644)
}
}