mirror of
https://github.com/celogeek/piwigo-cli.git
synced 2025-05-25 18:22:37 +02:00
save config, create subcommand
This commit is contained in:
parent
9c0dffad8d
commit
b902080534
60
internal/piwigo/config.go
Normal file
60
internal/piwigo/config.go
Normal file
@ -0,0 +1,60 @@
|
||||
package piwigo
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func (p *Piwigo) ConfigPath() (configPath string, err error) {
|
||||
configDir, err := os.UserConfigDir()
|
||||
if err == nil {
|
||||
configPath = fmt.Sprintf("%s/piwigo-cli", configDir)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (p *Piwigo) CreateConfigDir() (configPath string, err error) {
|
||||
configPath, err = p.ConfigPath()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = os.MkdirAll(configPath, os.FileMode(0700))
|
||||
return
|
||||
}
|
||||
|
||||
func (p *Piwigo) SaveConfig() (err error) {
|
||||
configPath, err := p.CreateConfigDir()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
configFile := fmt.Sprintf("%s/config.json", configPath)
|
||||
|
||||
b, err := json.MarshalIndent(p, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = os.WriteFile(configFile, b, os.FileMode(0700))
|
||||
return err
|
||||
}
|
||||
|
||||
func (p *Piwigo) LoadConfig() (err error) {
|
||||
configPath, err := p.ConfigPath()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
configFile := fmt.Sprintf("%s/config.json", configPath)
|
||||
b, err := os.ReadFile(configFile)
|
||||
if os.IsNotExist(err) {
|
||||
err = nil
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
err = json.Unmarshal(b, p)
|
||||
}
|
||||
return
|
||||
}
|
@ -8,58 +8,45 @@ import (
|
||||
"net/url"
|
||||
)
|
||||
|
||||
type Piwigo struct {
|
||||
Url string
|
||||
Token string
|
||||
Method string
|
||||
}
|
||||
func (p *Piwigo) BuildUrl(method string) (string, error) {
|
||||
|
||||
type PiwigoResult struct {
|
||||
Stat string `json:"stat"`
|
||||
Err int `json:"err"`
|
||||
ErrMessage string `json:"message"`
|
||||
Result interface{} `json:"result"`
|
||||
}
|
||||
|
||||
func (p *Piwigo) BuildUrl() (string, error) {
|
||||
|
||||
Url, Err := url.Parse(p.Url)
|
||||
if Err != nil {
|
||||
return "", Err
|
||||
Url, err := url.Parse(p.Url)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
Url.Path += "/ws.php"
|
||||
q := Url.Query()
|
||||
q.Set("format", "json")
|
||||
q.Set("method", p.Method)
|
||||
q.Set("method", method)
|
||||
Url.RawQuery = q.Encode()
|
||||
return Url.String(), nil
|
||||
}
|
||||
|
||||
func (p *Piwigo) Post(req *url.Values, resp interface{}) error {
|
||||
Url, Err := p.BuildUrl()
|
||||
if Err != nil {
|
||||
return Err
|
||||
func (p *Piwigo) Post(method string, req *url.Values, resp interface{}) error {
|
||||
Url, err := p.BuildUrl(method)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
r, Err := http.PostForm(Url, *req)
|
||||
if Err != nil {
|
||||
return Err
|
||||
r, err := http.PostForm(Url, *req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer r.Body.Close()
|
||||
|
||||
b, Err := ioutil.ReadAll(r.Body)
|
||||
if Err != nil {
|
||||
return Err
|
||||
b, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Result := PiwigoResult{
|
||||
Result: resp,
|
||||
}
|
||||
|
||||
Err = json.Unmarshal(b, &Result)
|
||||
if Err != nil {
|
||||
return Err
|
||||
err = json.Unmarshal(b, &Result)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if Result.Stat != "ok" {
|
||||
|
13
internal/piwigo/struct.go
Normal file
13
internal/piwigo/struct.go
Normal file
@ -0,0 +1,13 @@
|
||||
package piwigo
|
||||
|
||||
type Piwigo struct {
|
||||
Url string `json:"url"`
|
||||
Token string `json:"token"`
|
||||
}
|
||||
|
||||
type PiwigoResult struct {
|
||||
Stat string `json:"stat"`
|
||||
Err int `json:"err"`
|
||||
ErrMessage string `json:"message"`
|
||||
Result interface{} `json:"result"`
|
||||
}
|
@ -13,33 +13,43 @@ type LoginCommand struct {
|
||||
Password string `short:"p" long:"password" description:"Password"`
|
||||
}
|
||||
|
||||
var loginCommand LoginCommand
|
||||
type StatusCommand struct {
|
||||
}
|
||||
|
||||
type SessionGroup struct {
|
||||
Login LoginCommand `command:"login" description:"Initialize a connection to a piwigo instance"`
|
||||
Status StatusCommand `command:"status" description:"Get the status of your session"`
|
||||
}
|
||||
|
||||
var sessionGroup SessionGroup
|
||||
|
||||
func (c *LoginCommand) Execute(args []string) error {
|
||||
fmt.Printf("Login on %s...\n", c.Url)
|
||||
|
||||
Piwigo := piwigo.Piwigo{
|
||||
Url: c.Url,
|
||||
Method: "pwg.session.login",
|
||||
Url: c.Url,
|
||||
}
|
||||
|
||||
result := false
|
||||
|
||||
if Err := Piwigo.Post(&url.Values{
|
||||
err := Piwigo.Post("pwg.session.login", &url.Values{
|
||||
"username": []string{c.Login},
|
||||
"password": []string{c.Password},
|
||||
}, &result); Err != nil {
|
||||
return Err
|
||||
}, &result)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Printf("Token: %s\n", Piwigo.Token)
|
||||
err = Piwigo.SaveConfig()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Println("Login succeed!")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
parser.AddCommand("login",
|
||||
"Initialize a connection to a piwigo instance",
|
||||
"Initialize a connection to a piwigo instance",
|
||||
&loginCommand)
|
||||
parser.AddCommand("session", "Session management", "", &sessionGroup)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user