mirror of
https://github.com/usememos/memos.git
synced 2025-01-02 19:23:30 +03:00
chore: update commands (#2074)
This commit is contained in:
parent
aa26cc30d7
commit
cbe27923b3
123
cmd/memos.go
123
cmd/memos.go
@ -7,14 +7,12 @@ import (
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
|
||||
"github.com/usememos/memos/server"
|
||||
_profile "github.com/usememos/memos/server/profile"
|
||||
"github.com/usememos/memos/setup"
|
||||
"github.com/usememos/memos/store"
|
||||
"github.com/usememos/memos/store/db"
|
||||
)
|
||||
@ -81,110 +79,6 @@ var (
|
||||
<-ctx.Done()
|
||||
},
|
||||
}
|
||||
|
||||
setupCmd = &cobra.Command{
|
||||
Use: "setup",
|
||||
Short: "Make initial setup for memos",
|
||||
Run: func(cmd *cobra.Command, _ []string) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
hostUsername, err := cmd.Flags().GetString(setupCmdFlagHostUsername)
|
||||
if err != nil {
|
||||
fmt.Printf("failed to get owner username, error: %+v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
hostPassword, err := cmd.Flags().GetString(setupCmdFlagHostPassword)
|
||||
if err != nil {
|
||||
fmt.Printf("failed to get owner password, error: %+v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
db := db.NewDB(profile)
|
||||
if err := db.Open(ctx); err != nil {
|
||||
fmt.Printf("failed to open db, error: %+v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
store := store.New(db.DBInstance, profile)
|
||||
if err := setup.Execute(ctx, store, hostUsername, hostPassword); err != nil {
|
||||
fmt.Printf("failed to setup, error: %+v\n", err)
|
||||
return
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
mvrssCmd = &cobra.Command{
|
||||
Use: "mvrss", // `mvrss` is a shortened for 'means move resource'
|
||||
Short: "Move resource between storage",
|
||||
Run: func(cmd *cobra.Command, _ []string) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
from, err := cmd.Flags().GetString(mvrssCmdFlagFrom)
|
||||
if err != nil {
|
||||
fmt.Printf("failed to get from storage, error: %+v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
to, err := cmd.Flags().GetString(mvrssCmdFlagTo)
|
||||
if err != nil {
|
||||
fmt.Printf("failed to get to storage, error: %+v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
if from != "local" || to != "db" {
|
||||
fmt.Printf("only local=>db be supported currently\n")
|
||||
return
|
||||
}
|
||||
|
||||
db := db.NewDB(profile)
|
||||
if err := db.Open(ctx); err != nil {
|
||||
fmt.Printf("failed to open db, error: %+v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
s := store.New(db.DBInstance, profile)
|
||||
resources, err := s.ListResources(ctx, &store.FindResource{})
|
||||
if err != nil {
|
||||
fmt.Printf("failed to list resources, error: %+v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
var emptyString string
|
||||
for _, res := range resources {
|
||||
if res.InternalPath == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
buf, err := os.ReadFile(res.InternalPath)
|
||||
if err != nil {
|
||||
fmt.Printf("Resource %5d failed to read file: %s\n", res.ID, err)
|
||||
continue
|
||||
}
|
||||
|
||||
if len(buf) != int(res.Size) {
|
||||
fmt.Printf("Resource %5d size of file %d != %d\n", res.ID, len(buf), res.Size)
|
||||
continue
|
||||
}
|
||||
|
||||
update := store.UpdateResource{
|
||||
ID: res.ID,
|
||||
Blob: buf,
|
||||
InternalPath: &emptyString,
|
||||
}
|
||||
_, err = s.UpdateResource(ctx, &update)
|
||||
if err != nil {
|
||||
fmt.Printf("Resource %5d failed to update: %s\n", res.ID, err)
|
||||
continue
|
||||
}
|
||||
|
||||
fmt.Printf("Resource %5d copy %12d bytes from %s\n", res.ID, len(buf), res.InternalPath)
|
||||
}
|
||||
fmt.Println("done")
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
func Execute() error {
|
||||
@ -214,16 +108,6 @@ func init() {
|
||||
viper.SetDefault("mode", "demo")
|
||||
viper.SetDefault("port", 8081)
|
||||
viper.SetEnvPrefix("memos")
|
||||
|
||||
setupCmd.Flags().String(setupCmdFlagHostUsername, "", "Owner username")
|
||||
setupCmd.Flags().String(setupCmdFlagHostPassword, "", "Owner password")
|
||||
|
||||
rootCmd.AddCommand(setupCmd)
|
||||
|
||||
mvrssCmd.Flags().String(mvrssCmdFlagFrom, "local", "From storage")
|
||||
mvrssCmd.Flags().String(mvrssCmdFlagTo, "db", "To Storage")
|
||||
|
||||
rootCmd.AddCommand(mvrssCmd)
|
||||
}
|
||||
|
||||
func initConfig() {
|
||||
@ -243,10 +127,3 @@ func initConfig() {
|
||||
println("version:", profile.Version)
|
||||
println("---")
|
||||
}
|
||||
|
||||
const (
|
||||
setupCmdFlagHostUsername = "host-username"
|
||||
setupCmdFlagHostPassword = "host-password"
|
||||
mvrssCmdFlagFrom = "from"
|
||||
mvrssCmdFlagTo = "to"
|
||||
)
|
||||
|
94
cmd/mvrss.go
Normal file
94
cmd/mvrss.go
Normal file
@ -0,0 +1,94 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/usememos/memos/store"
|
||||
"github.com/usememos/memos/store/db"
|
||||
)
|
||||
|
||||
var (
|
||||
mvrssCmdFlagFrom = "from"
|
||||
mvrssCmdFlagTo = "to"
|
||||
mvrssCmd = &cobra.Command{
|
||||
Use: "mvrss", // `mvrss` is a shortened for 'means move resource'
|
||||
Short: "Move resource between storage",
|
||||
Run: func(cmd *cobra.Command, _ []string) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
from, err := cmd.Flags().GetString(mvrssCmdFlagFrom)
|
||||
if err != nil {
|
||||
fmt.Printf("failed to get from storage, error: %+v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
to, err := cmd.Flags().GetString(mvrssCmdFlagTo)
|
||||
if err != nil {
|
||||
fmt.Printf("failed to get to storage, error: %+v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
if from != "local" || to != "db" {
|
||||
fmt.Printf("only local=>db be supported currently\n")
|
||||
return
|
||||
}
|
||||
|
||||
db := db.NewDB(profile)
|
||||
if err := db.Open(ctx); err != nil {
|
||||
fmt.Printf("failed to open db, error: %+v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
s := store.New(db.DBInstance, profile)
|
||||
resources, err := s.ListResources(ctx, &store.FindResource{})
|
||||
if err != nil {
|
||||
fmt.Printf("failed to list resources, error: %+v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
var emptyString string
|
||||
for _, res := range resources {
|
||||
if res.InternalPath == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
buf, err := os.ReadFile(res.InternalPath)
|
||||
if err != nil {
|
||||
fmt.Printf("Resource %5d failed to read file: %s\n", res.ID, err)
|
||||
continue
|
||||
}
|
||||
|
||||
if len(buf) != int(res.Size) {
|
||||
fmt.Printf("Resource %5d size of file %d != %d\n", res.ID, len(buf), res.Size)
|
||||
continue
|
||||
}
|
||||
|
||||
update := store.UpdateResource{
|
||||
ID: res.ID,
|
||||
Blob: buf,
|
||||
InternalPath: &emptyString,
|
||||
}
|
||||
_, err = s.UpdateResource(ctx, &update)
|
||||
if err != nil {
|
||||
fmt.Printf("Resource %5d failed to update: %s\n", res.ID, err)
|
||||
continue
|
||||
}
|
||||
|
||||
fmt.Printf("Resource %5d copy %12d bytes from %s\n", res.ID, len(buf), res.InternalPath)
|
||||
}
|
||||
fmt.Println("done")
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
func init() {
|
||||
mvrssCmd.Flags().String(mvrssCmdFlagFrom, "local", "From storage")
|
||||
mvrssCmd.Flags().String(mvrssCmdFlagTo, "db", "To Storage")
|
||||
|
||||
rootCmd.AddCommand(mvrssCmd)
|
||||
}
|
@ -1,17 +1,63 @@
|
||||
package setup
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/usememos/memos/common/util"
|
||||
"github.com/usememos/memos/store"
|
||||
"github.com/usememos/memos/store/db"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
func Execute(ctx context.Context, store *store.Store, hostUsername, hostPassword string) error {
|
||||
var (
|
||||
setupCmdFlagHostUsername = "host-username"
|
||||
setupCmdFlagHostPassword = "host-password"
|
||||
setupCmd = &cobra.Command{
|
||||
Use: "setup",
|
||||
Short: "Make initial setup for memos",
|
||||
Run: func(cmd *cobra.Command, _ []string) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
hostUsername, err := cmd.Flags().GetString(setupCmdFlagHostUsername)
|
||||
if err != nil {
|
||||
fmt.Printf("failed to get owner username, error: %+v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
hostPassword, err := cmd.Flags().GetString(setupCmdFlagHostPassword)
|
||||
if err != nil {
|
||||
fmt.Printf("failed to get owner password, error: %+v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
db := db.NewDB(profile)
|
||||
if err := db.Open(ctx); err != nil {
|
||||
fmt.Printf("failed to open db, error: %+v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
store := store.New(db.DBInstance, profile)
|
||||
if err := ExecuteSetup(ctx, store, hostUsername, hostPassword); err != nil {
|
||||
fmt.Printf("failed to setup, error: %+v\n", err)
|
||||
return
|
||||
}
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
func init() {
|
||||
setupCmd.Flags().String(setupCmdFlagHostUsername, "", "Owner username")
|
||||
setupCmd.Flags().String(setupCmdFlagHostPassword, "", "Owner password")
|
||||
|
||||
rootCmd.AddCommand(setupCmd)
|
||||
}
|
||||
|
||||
func ExecuteSetup(ctx context.Context, store *store.Store, hostUsername, hostPassword string) error {
|
||||
s := setupService{store: store}
|
||||
return s.Setup(ctx, hostUsername, hostPassword)
|
||||
}
|
Loading…
Reference in New Issue
Block a user