x
This commit is contained in:
40
cmd/chatlog/cmd_decrypt.go
Normal file
40
cmd/chatlog/cmd_decrypt.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package chatlog
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/sjzar/chatlog/internal/chatlog"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(decryptCmd)
|
||||
decryptCmd.Flags().StringVarP(&dataDir, "data-dir", "d", "", "data dir")
|
||||
decryptCmd.Flags().StringVarP(&workDir, "work-dir", "w", "", "work dir")
|
||||
decryptCmd.Flags().StringVarP(&key, "key", "k", "", "key")
|
||||
decryptCmd.Flags().IntVarP(&decryptVer, "version", "v", 3, "version")
|
||||
}
|
||||
|
||||
var dataDir string
|
||||
var workDir string
|
||||
var key string
|
||||
var decryptVer int
|
||||
|
||||
var decryptCmd = &cobra.Command{
|
||||
Use: "decrypt",
|
||||
Short: "decrypt",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
m, err := chatlog.New("")
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return
|
||||
}
|
||||
if err := m.CommandDecrypt(dataDir, workDir, key, decryptVer); err != nil {
|
||||
log.Error(err)
|
||||
return
|
||||
}
|
||||
fmt.Println("decrypt success")
|
||||
},
|
||||
}
|
||||
34
cmd/chatlog/cmd_key.go
Normal file
34
cmd/chatlog/cmd_key.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package chatlog
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/sjzar/chatlog/internal/chatlog"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(keyCmd)
|
||||
keyCmd.Flags().IntVarP(&pid, "pid", "p", 0, "pid")
|
||||
}
|
||||
|
||||
var pid int
|
||||
var keyCmd = &cobra.Command{
|
||||
Use: "key",
|
||||
Short: "key",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
m, err := chatlog.New("")
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return
|
||||
}
|
||||
ret, err := m.CommandKey(pid)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return
|
||||
}
|
||||
fmt.Println(ret)
|
||||
},
|
||||
}
|
||||
27
cmd/chatlog/cmd_version.go
Normal file
27
cmd/chatlog/cmd_version.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package chatlog
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/sjzar/chatlog/pkg/version"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(versionCmd)
|
||||
versionCmd.Flags().BoolVarP(&versionM, "module", "m", false, "module version information")
|
||||
}
|
||||
|
||||
var versionM bool
|
||||
var versionCmd = &cobra.Command{
|
||||
Use: "version [-m]",
|
||||
Short: "Show the version of chatlog",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
if versionM {
|
||||
fmt.Println(version.GetMore(true))
|
||||
} else {
|
||||
fmt.Printf("chatlog %s\n", version.GetMore(false))
|
||||
}
|
||||
},
|
||||
}
|
||||
50
cmd/chatlog/log.go
Normal file
50
cmd/chatlog/log.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package chatlog
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
|
||||
"github.com/sjzar/chatlog/pkg/util"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var Debug bool
|
||||
|
||||
func initLog(cmd *cobra.Command, args []string) {
|
||||
log.SetFormatter(&log.TextFormatter{
|
||||
FullTimestamp: true,
|
||||
CallerPrettyfier: func(f *runtime.Frame) (string, string) {
|
||||
_, filename := path.Split(f.File)
|
||||
return "", fmt.Sprintf("%s:%d", filename, f.Line)
|
||||
},
|
||||
})
|
||||
|
||||
if Debug {
|
||||
log.SetLevel(log.DebugLevel)
|
||||
log.SetReportCaller(true)
|
||||
}
|
||||
}
|
||||
|
||||
func initTuiLog(cmd *cobra.Command, args []string) {
|
||||
logOutput := io.Discard
|
||||
|
||||
debug, _ := cmd.Flags().GetBool("debug")
|
||||
if debug {
|
||||
logpath := util.DefaultWorkDir("")
|
||||
util.PrepareDir(logpath)
|
||||
logFD, err := os.OpenFile(filepath.Join(logpath, "chatlog.log"), os.O_CREATE|os.O_WRONLY|os.O_APPEND, os.ModePerm)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
logOutput = logFD
|
||||
log.SetReportCaller(true)
|
||||
}
|
||||
|
||||
log.SetOutput(logOutput)
|
||||
}
|
||||
48
cmd/chatlog/root.go
Normal file
48
cmd/chatlog/root.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package chatlog
|
||||
|
||||
import (
|
||||
"github.com/sjzar/chatlog/internal/chatlog"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func init() {
|
||||
// windows only
|
||||
cobra.MousetrapHelpText = ""
|
||||
|
||||
rootCmd.PersistentFlags().BoolVar(&Debug, "debug", false, "debug")
|
||||
rootCmd.PersistentPreRun = initLog
|
||||
}
|
||||
|
||||
func Execute() {
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "chatlog",
|
||||
Short: "chatlog",
|
||||
Long: `chatlog`,
|
||||
Example: `chatlog`,
|
||||
Args: cobra.MinimumNArgs(0),
|
||||
CompletionOptions: cobra.CompletionOptions{
|
||||
HiddenDefaultCmd: true,
|
||||
},
|
||||
PreRun: initTuiLog,
|
||||
Run: Root,
|
||||
}
|
||||
|
||||
func Root(cmd *cobra.Command, args []string) {
|
||||
|
||||
m, err := chatlog.New("")
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := m.Run(); err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user