Switch to Cobra

Also:
- Fixed the Makefile
- Added a Taskfile.yml (see: https://taskfile.dev)
- Updated the manual page

Signed-off-by: Shin'ya Minazuki <shinyoukai@laidback.moe>
This commit is contained in:
2025-12-30 12:10:46 -03:00
parent 60688d7a0e
commit f045e36119
12 changed files with 168 additions and 84 deletions

View File

@@ -2,14 +2,15 @@ package main
import (
"fmt"
"log"
"os"
"strings"
"syscall"
"golang.org/x/term"
"go.yarn.social/client"
"git.laidback.moe/shinyoukai/mikuru"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var (
@@ -17,37 +18,38 @@ var (
password string
)
var loginCmd = &cobra.Command{
Use: "login",
Short: "Authenticate against a Yarn.social pod",
Aliases: []string{"auth", "signin"},
Args: cobra.MaximumNArgs(0),
Run: func(_ *cobra.Command, _ []string) {
cli, err := client.NewClient(client.WithURI(mikuru.Config.Host))
if err != nil {
log.Fatal("Unable to create client", err)
}
signin(cli)
},
}
func init() {
mikuru.ConfInit()
rootCmd.AddCommand(loginCmd)
}
func main() {
cli, err := client.NewClient(client.WithURI(mikuru.Config.Host))
if err != nil {
log.Println("Unable to create client")
log.Fatal(err)
}
fmt.Println("Welcome to Mikuru!")
fmt.Printf("%s\n", mikuru.PrintVersion())
signin(cli)
}
func signin(cli *client.Client) {
fmt.Printf("Username: ")
fmt.Scanln(&username)
if len(username) == 0 {
fmt.Println("No value. Bailing out")
os.Exit(1)
log.Fatal("No value. Bailing out")
}
fmt.Printf("Password: ")
data, err := term.ReadPassword(int(syscall.Stdin))
if err != nil {
fmt.Printf("Unable to obtain password: %s\n", err)
os.Exit(1)
log.Fatal("Unable to obtain password", err)
}
password := string(data)
@@ -55,15 +57,14 @@ func signin(cli *client.Client) {
res, err := cli.Login(username, password)
if err != nil {
fmt.Println("Unable to login")
os.Exit(1)
log.Fatal("Unable to login", err)
}
token := strings.Trim(fmt.Sprintf(res.Token), "{}")
fmt.Println("Login successful")
fmt.Println("Place this token in your configuration file for later use")
fmt.Println("Do not share it with anyone, it's classified information")
fmt.Println("Do not share it with anyone, it's classified information!")
fmt.Println("")
fmt.Printf("token = %v\n", token)
}

5
cmd/main.go Normal file
View File

@@ -0,0 +1,5 @@
package main
func main() {
Execute()
}

View File

@@ -1,43 +0,0 @@
package main
import (
"log"
"git.laidback.moe/shinyoukai/mikuru"
"go.yarn.social/client"
"github.com/tj/go-editor"
)
func init() {
mikuru.ConfInit()
}
func main() {
cli, err := client.NewClient(
client.WithURI(mikuru.Config.Host),
client.WithToken(mikuru.Config.Token),
)
if err != nil {
log.Fatal(err)
}
write(cli)
}
func write(cli *client.Client) {
var post string
data, err := editor.Read()
if err != nil {
log.Fatal("Unable to read content from editor")
}
post = string(data)
_, err = cli.Post(post, "")
if err != nil {
log.Fatal("Unable to publish tweet")
}
}

48
cmd/post.go Normal file
View File

@@ -0,0 +1,48 @@
package main
import (
"git.laidback.moe/shinyoukai/mikuru"
"go.yarn.social/client"
"github.com/tj/go-editor"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var postCmd = &cobra.Command{
Use: "post",
Aliases: []string{"tweet"},
Short: "Publish a new post to a Yarn pod",
Run: func(_ *cobra.Command, args []string) {
cli, err := client.NewClient(
client.WithURI(mikuru.Config.Host),
client.WithToken(mikuru.Config.Token),
)
if err != nil {
log.Fatal("Unable to create client", err)
}
write(cli)
},
}
func init() {
rootCmd.AddCommand(postCmd)
mikuru.ConfInit()
}
func write(cli *client.Client) {
var post string
data, err := editor.Read()
if err != nil {
log.Fatal("Unable to read content from editor", err)
}
post = string(data)
_, err = cli.Post(post, "")
if err != nil {
log.Fatal("Unable to publish tweet", err)
}
}

20
cmd/root.go Normal file
View File

@@ -0,0 +1,20 @@
package main
import (
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"git.laidback.moe/shinyoukai/mikuru"
)
var rootCmd = &cobra.Command{
Use: "mikuru",
Short: "A client for Yarn.social from the future",
Version: mikuru.Version,
}
func Execute() {
err := rootCmd.Execute()
if err != nil {
log.Fatal(err)
}
}