File closure fixed on init

This commit is contained in:
Jguer 2017-10-19 14:59:26 +09:00
parent a46e6bfa7b
commit 58ab6fd06f
5 changed files with 80 additions and 80 deletions

14
cmd.go
View File

@ -86,12 +86,12 @@ func init() {
// Save the default config if nothing is found
config.saveConfig()
} else {
file, err := os.OpenFile(configFile, os.O_RDWR|os.O_CREATE, 0644)
cfile, err := os.OpenFile(configFile, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
fmt.Println("Error reading config:", err)
} else {
defer file.Close()
decoder := json.NewDecoder(file)
defer cfile.Close()
decoder := json.NewDecoder(cfile)
err = decoder.Decode(&config)
if err != nil {
fmt.Println("Loading default Settings.\nError reading config:", err)
@ -105,10 +105,10 @@ func init() {
////////////////
updated = false
file, err := os.OpenFile(vcsFile, os.O_RDWR|os.O_CREATE, 0644)
vfile, err := os.Open(vcsFile)
if err == nil {
defer file.Close()
decoder := json.NewDecoder(file)
defer vfile.Close()
decoder := json.NewDecoder(vfile)
_ = decoder.Decode(&savedInfo)
}
@ -216,7 +216,7 @@ func main() {
}
}
case "-Qstats":
err = localStatistics(version)
err = localStatistics()
case "-Ss", "-Ssq", "-Sqs":
if op == "-Ss" {
config.SearchMode = Detailed

View File

@ -41,7 +41,7 @@ type Configuration struct {
Devel bool `json:"devel"`
}
var version = "2.201"
const version = "2.201"
// baseURL givers the AUR default address.
const baseURL string = "https://aur.archlinux.org"

View File

@ -178,3 +178,74 @@ func biggestPackages() {
}
// Could implement size here as well, but we just want the general idea
}
// localStatistics prints installed packages statistics.
func localStatistics() error {
info, err := statistics()
if err != nil {
return err
}
_, _, _, remoteNames, err := filterPackages()
if err != nil {
return err
}
fmt.Printf("\n Yay version r%s\n", version)
fmt.Println("\x1B[1;34m===========================================\x1B[0m")
fmt.Printf("\x1B[1;32mTotal installed packages: \x1B[0;33m%d\x1B[0m\n", info.Totaln)
fmt.Printf("\x1B[1;32mTotal foreign installed packages: \x1B[0;33m%d\x1B[0m\n", len(remoteNames))
fmt.Printf("\x1B[1;32mExplicitly installed packages: \x1B[0;33m%d\x1B[0m\n", info.Expln)
fmt.Printf("\x1B[1;32mTotal Size occupied by packages: \x1B[0;33m%s\x1B[0m\n", human(info.TotalSize))
fmt.Println("\x1B[1;34m===========================================\x1B[0m")
fmt.Println("\x1B[1;32mTen biggest packages\x1B[0m")
biggestPackages()
fmt.Println("\x1B[1;34m===========================================\x1B[0m")
var q aurQuery
var j int
for i := len(remoteNames); i != 0; i = j {
j = i - config.RequestSplitN
if j < 0 {
j = 0
}
qtemp, err := rpc.Info(remoteNames[j:i])
q = append(q, qtemp...)
if err != nil {
return err
}
}
var outcast []string
for _, s := range remoteNames {
found := false
for _, i := range q {
if s == i.Name {
found = true
break
}
}
if !found {
outcast = append(outcast, s)
}
}
if err != nil {
return err
}
for _, res := range q {
if res.Maintainer == "" {
fmt.Printf("\x1b[1;31;40mWarning: \x1B[1;33;40m%s\x1b[0;37;40m is orphaned.\x1b[0m\n", res.Name)
}
if res.OutOfDate != 0 {
fmt.Printf("\x1b[1;31;40mWarning: \x1B[1;33;40m%s\x1b[0;37;40m is out-of-date in AUR.\x1b[0m\n", res.Name)
}
}
for _, res := range outcast {
fmt.Printf("\x1b[1;31;40mWarning: \x1B[1;33;40m%s\x1b[0;37;40m is not available in AUR.\x1b[0m\n", res)
}
return nil
}

View File

@ -175,77 +175,6 @@ func syncInfo(pkgS []string, flags []string) (err error) {
return
}
// LocalStatistics returns installed packages statistics.
func localStatistics(version string) error {
info, err := statistics()
if err != nil {
return err
}
_, _, _, remoteNames, err := filterPackages()
if err != nil {
return err
}
fmt.Printf("\n Yay version r%s\n", version)
fmt.Println("\x1B[1;34m===========================================\x1B[0m")
fmt.Printf("\x1B[1;32mTotal installed packages: \x1B[0;33m%d\x1B[0m\n", info.Totaln)
fmt.Printf("\x1B[1;32mTotal foreign installed packages: \x1B[0;33m%d\x1B[0m\n", len(remoteNames))
fmt.Printf("\x1B[1;32mExplicitly installed packages: \x1B[0;33m%d\x1B[0m\n", info.Expln)
fmt.Printf("\x1B[1;32mTotal Size occupied by packages: \x1B[0;33m%s\x1B[0m\n", human(info.TotalSize))
fmt.Println("\x1B[1;34m===========================================\x1B[0m")
fmt.Println("\x1B[1;32mTen biggest packages\x1B[0m")
biggestPackages()
fmt.Println("\x1B[1;34m===========================================\x1B[0m")
var q aurQuery
var j int
for i := len(remoteNames); i != 0; i = j {
j = i - config.RequestSplitN
if j < 0 {
j = 0
}
qtemp, err := rpc.Info(remoteNames[j:i])
q = append(q, qtemp...)
if err != nil {
return err
}
}
var outcast []string
for _, s := range remoteNames {
found := false
for _, i := range q {
if s == i.Name {
found = true
break
}
}
if !found {
outcast = append(outcast, s)
}
}
if err != nil {
return err
}
for _, res := range q {
if res.Maintainer == "" {
fmt.Printf("\x1b[1;31;40mWarning: \x1B[1;33;40m%s\x1b[0;37;40m is orphaned.\x1b[0m\n", res.Name)
}
if res.OutOfDate != 0 {
fmt.Printf("\x1b[1;31;40mWarning: \x1B[1;33;40m%s\x1b[0;37;40m is out-of-date in AUR.\x1b[0m\n", res.Name)
}
}
for _, res := range outcast {
fmt.Printf("\x1b[1;31;40mWarning: \x1B[1;33;40m%s\x1b[0;37;40m is not available in AUR.\x1b[0m\n", res)
}
return nil
}
// Search handles repo searches. Creates a RepoSearch struct.
func queryRepo(pkgInputN []string) (s repoQuery, n int, err error) {
dbList, err := alpmHandle.SyncDbs()

2
vcs.go
View File

@ -129,7 +129,7 @@ func saveVCSInfo() error {
if err != nil || string(marshalledinfo) == "null" {
return err
}
in, err := os.OpenFile(vcsFile, os.O_RDWR|os.O_CREATE, 0755)
in, err := os.OpenFile(vcsFile, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
return err
}