slp/operators/show.go

53 lines
1.0 KiB
Go

package operators
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
)
func Show(pkg string) error {
modDir := GetModBaseDir()
path := filepath.Join(modDir, pkg)
if _, err := os.Stat(path); err != nil {
// If the package is not locally available
// get it from the registry
err := checkRegistry()
if err != nil {
return err
}
packages, err := getPackages()
if err != nil {
return err
}
pakg, ok := packages[pkg]
if !ok {
return fmt.Errorf("\033[33;1m%s\033[0m\n \033[2m└\033[0m \033[91mError:\033[0m package '%s' not found", pkg, pkg)
}
fmt.Println(pakg.String())
return nil
}
path = filepath.Join(path, "module.json")
if _, err := os.Stat(path); err != nil {
return fmt.Errorf("\033[33;1m%s\033[0m\n \033[2m└\033[0m \033[91mError:\033[0m package '%s' does not have a 'module.json' file", pkg, pkg)
}
bytes, err := ioutil.ReadFile(path)
if err != nil {
return err
}
var info genOpts
err = json.Unmarshal(bytes, &info)
if err != nil {
return err
}
fmt.Println(info.String())
return nil
}