diff --git a/README.md b/README.md index bbb9ae1..ce09a7e 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ # terminal_note -`terminal_note` is a terminal notebook app, in which you can record some notes in your terminal and set password for it. +`terminal_note` is a terminal notebook app with password, in which you can record some notes in your terminal. ## Install ```go -$ cd $GOPATH && git clone https://github.com/JemmyH/terminal_note.git +$ cd $GOPATH && mkdir jemmyh && cd jemmyh && git clone https://github.com/JemmyH/terminal_note.git $ cd terminal_note && go build -o tnote && mv tnote $GOPATH/bin/ ``` diff --git a/cmd/add.go b/cmd/add.go index 805a62d..fcd2ec7 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -16,8 +16,8 @@ limitations under the License. package cmd import ( + "github.com/jemmyh/terminal_note/note" "github.com/spf13/cobra" - "hujm.net/terminal_note/note" ) var content string diff --git a/cmd/create.go b/cmd/create.go index b8b3c51..06d0e43 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -18,8 +18,8 @@ package cmd import ( "fmt" + "github.com/jemmyh/terminal_note/note" "github.com/spf13/cobra" - "hujm.net/terminal_note/note" ) // createCmd represents the create command @@ -39,14 +39,4 @@ var createCmd = &cobra.Command{ func init() { rootCmd.AddCommand(createCmd) - - // Here you will define your flags and configuration settings. - - // Cobra supports Persistent Flags which will work for this command - // and all subcommands, e.g.: - // createCmd.PersistentFlags().String("foo", "", "A help for foo") - - // Cobra supports local flags which will only run when this command - // is called directly, e.g.: - // createCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") } diff --git a/cmd/delete.go b/cmd/delete.go index df4fdbc..04eec0b 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -16,8 +16,8 @@ limitations under the License. package cmd import ( + "github.com/jemmyh/terminal_note/note" "github.com/spf13/cobra" - "hujm.net/terminal_note/note" ) var ( diff --git a/cmd/print.go b/cmd/print.go index a08f954..0f3ccde 100644 --- a/cmd/print.go +++ b/cmd/print.go @@ -20,8 +20,8 @@ import ( "fmt" "github.com/boltdb/bolt" + "github.com/jemmyh/terminal_note/note" "github.com/spf13/cobra" - "hujm.net/terminal_note/note" ) var ( @@ -99,6 +99,7 @@ func (p *printStruct) checkBreak(id []byte) bool { func (p *printStruct) printNote(n *note.Note) { if p.verbose { fmt.Println(n.String()) + // TODO: use arrow // if p.reverse && n.PrevID != nil { // fmt.Println("⇧") // fmt.Println("|") @@ -115,11 +116,10 @@ func (p *printStruct) printNote(n *note.Note) { var printCmd = &cobra.Command{ Use: "print", Short: "Print notes in your notebook", - PreRun: func(cmd *cobra.Command, args []string) { + PreRun: func(_ *cobra.Command, _ []string) { CheckNotebook(GetUserName()) }, - Run: func(cmd *cobra.Command, args []string) { - fmt.Printf("user: %s, myP:%+v \n", GetUserName(), myP) + Run: func(_ *cobra.Command, _ []string) { userName := GetUserName() passwd := GetInputPassword() switch myP.printType { @@ -144,7 +144,7 @@ var printCmd = &cobra.Command{ } } case "range": - + // TODO: range print default: fmt.Println("invalid type: " + myP.printType) return diff --git a/cmd/root.go b/cmd/root.go index aa3c45c..f4ef7a3 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -6,14 +6,14 @@ import ( "os" "os/user" + "github.com/jemmyh/terminal_note/note" "github.com/spf13/cobra" "gopkg.in/AlecAivazis/survey.v1" - "hujm.net/terminal_note/note" ) /* * @CreateTime: 2021/1/12 20:59 -* @Author: hujiaming +* @Author: JemmyHu * @Description: */ @@ -31,13 +31,14 @@ var rootCmd = &cobra.Command{ Long: `Terminal Notebook is CLI App, which is implemented by Golang.`, } +// Execute ... func Execute() { if err := rootCmd.Execute(); err != nil { os.Exit(0) } } -// GetUserName ... +// GetUserName returns the current logined user, the same as `whoami` func GetUserName() string { if userName == "" { u, _ := user.Current() @@ -46,6 +47,7 @@ func GetUserName() string { return userName } +// CheckNotebook make app exit if user's notebook does not exist. func CheckNotebook(userName string) { if !note.CheckDbFileExist(userName) { fmt.Printf("No existing notebook found for %s. Use `create` to create one.\n", userName) @@ -53,6 +55,7 @@ func CheckNotebook(userName string) { } } +// GetInputPassword gets password from stdin. func GetInputPassword() string { password := "" prompt := &survey.Password{Message: "Please type your password:"} diff --git a/cmd/version.go b/cmd/version.go index 9c28c59..967dda9 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -18,8 +18,8 @@ package cmd import ( "fmt" + "github.com/jemmyh/terminal_note/note" "github.com/spf13/cobra" - "hujm.net/terminal_note/note" ) // versionCmd represents the version command @@ -35,6 +35,7 @@ func init() { rootCmd.AddCommand(versionCmd) } +// GetVersionStr returns the formated version string. func GetVersionStr() string { return fmt.Sprintf("%s @%s", note.GetVersion(), note.GetAppName()) } diff --git a/main.go b/main.go index 55b90d2..d33d423 100644 --- a/main.go +++ b/main.go @@ -4,7 +4,7 @@ import "github.com/jemmyh/terminal_note/cmd" /* * @CreateTime: 2021/1/14 15:31 -* @Author: hujiaming +* @Author: JemmyHu * @Description: */ diff --git a/note/note.go b/note/note.go index 927919e..09c138a 100644 --- a/note/note.go +++ b/note/note.go @@ -15,7 +15,7 @@ import ( /* * @CreateTime: 2021/1/12 18:28 * @Author: Jemmy@hujm20150121@gmail.com -* @Description: +* @Description: note is a message in notebook */ // Note means a diary note diff --git a/note/note_book.go b/note/note_book.go index b87c1a5..9a596f1 100644 --- a/note/note_book.go +++ b/note/note_book.go @@ -15,7 +15,7 @@ import ( /* * @CreateTime: 2021/1/12 18:30 * @Author: Jemmy@hujm20150121@gmail.com -* @Description: +* @Description: notebook */ // NoteBook represents a diary book that contains many diary notes. diff --git a/note/note_book_test.go b/note/note_book_test.go index 38e4775..5d81180 100644 --- a/note/note_book_test.go +++ b/note/note_book_test.go @@ -8,7 +8,7 @@ import ( /* * @CreateTime: 2021/1/13 11:41 -* @Author: hujiaming +* @Author: JemmyHu * @Description: */ diff --git a/note/note_iter.go b/note/note_iter.go index 32ca354..7f29892 100644 --- a/note/note_iter.go +++ b/note/note_iter.go @@ -8,7 +8,7 @@ import ( /* * @CreateTime: 2021/1/12 20:49 -* @Author: hujiaming +* @Author: JemmyHu * @Description: a iterator for note */ diff --git a/note/note_iter_test.go b/note/note_iter_test.go index c31e297..554e0e3 100644 --- a/note/note_iter_test.go +++ b/note/note_iter_test.go @@ -4,7 +4,7 @@ import "testing" /* * @CreateTime: 2021/1/13 16:09 -* @Author: hujiaming +* @Author: JemmyHu * @Description: */ diff --git a/note/note_test.go b/note/note_test.go index 0f92dc7..6f9fb1d 100644 --- a/note/note_test.go +++ b/note/note_test.go @@ -3,13 +3,11 @@ package note import ( "testing" "time" - - "github.com/stretchr/testify/assert" ) /* * @CreateTime: 2021/1/13 11:31 -* @Author: hujiaming +* @Author: JemmyHu * @Description: */ @@ -23,17 +21,3 @@ func TestNewNote(t *testing.T) { note = NewNote("test content") t.Log(note.String()) } - -func TestNote_Serialize(t *testing.T) { - data := note.Serialize() - t.Logf("data: %s", data) - - newNote := DeserializeNote(data) - t.Log(newNote.String()) - assert.Equal(t, note.String(), newNote.String()) -} - -func TestGetHeadNote(t *testing.T) { - head := GetHeadNote() - t.Log(head.String()) -} diff --git a/note/rsa.go b/note/rsa.go index 996eb7e..7ab9586 100644 --- a/note/rsa.go +++ b/note/rsa.go @@ -9,26 +9,26 @@ import ( /* * @CreateTime: 2021/1/13 18:58 -* @Author: hujiaming +* @Author: JemmyHu * @Description: rsa -1. GenRsaKey 得到公私钥 -2. RsaEncrypt 传入要加密的数据 和 公钥,进行加密,得到加密后的数据 -3. RsaDecrypt 传入加密后的数据和私钥解密,得到解密后的数据 +1. GenRsaKey returns publicKey and privateKey +2. RsaEncrypt input data needed to encrypt and publicKey, output data that has been encrypted +3. RsaDecrypt input encrypted data and privateKey, output the decrypted data */ // RSAGenKey generate rsa key pair. func RSAGenKey(bits int) (pubKey, prvKey []byte, err error) { /* - 生成私钥 + generate privateKey */ - // 1、使用RSA中的GenerateKey方法生成私钥 + // 1、get a privateKey privateKey, err := rsa.GenerateKey(rand.Reader, bits) if err != nil { return nil, nil, err } - // 2、通过X509标准将得到的RAS私钥序列化为:ASN.1 的DER编码字符串 + // 2、Marshal the privateKey privateStream := x509.MarshalPKCS1PrivateKey(privateKey) - // 3、将私钥字符串设置到pem格式块中 + // 3、put the marshaled privateKey into a Block block1 := &pem.Block{ Type: "private key", Bytes: privateStream, @@ -36,7 +36,7 @@ func RSAGenKey(bits int) (pubKey, prvKey []byte, err error) { prvKey = pem.EncodeToMemory(block1) /* - 生成公钥 + genarate publicKey from privateKey */ publicKey := privateKey.PublicKey publicStream, err := x509.MarshalPKIXPublicKey(&publicKey) @@ -52,24 +52,24 @@ func RSAGenKey(bits int) (pubKey, prvKey []byte, err error) { func RSAEncrypt(src []byte, pubKey []byte) (res []byte, err error) { block, _ := pem.Decode(pubKey) - // 使用X509将解码之后的数据 解析出来 + // unmarshal publicKey keyInit, err := x509.ParsePKIXPublicKey(block.Bytes) if err != nil { return } publicKey := keyInit.(*rsa.PublicKey) - // 使用公钥加密数据 + // encrypt data with publicKey res, err = rsa.EncryptPKCS1v15(rand.Reader, publicKey, src) return } // RSADecrypt decrypt encrypted data with private key. func RSADecrypt(encryptedSrc []byte, prvKey []byte) (res []byte, err error) { - // 解码 + // decode the privateKey block, _ := pem.Decode(prvKey) blockBytes := block.Bytes privateKey, err := x509.ParsePKCS1PrivateKey(blockBytes) - // 还原数据 + // decrypt by privateKey res, err = rsa.DecryptPKCS1v15(rand.Reader, privateKey, encryptedSrc) return } @@ -78,21 +78,18 @@ func RSADecrypt(encryptedSrc []byte, prvKey []byte) (res []byte, err error) { // RSAGenKeyWithPwd generate rsa pair key with specified password func RSAGenKeyWithPwd(bits int, pwd string) (pubKey, prvKey []byte, err error) { /* - 生成私钥 + generate privateKey */ - // 1、使用RSA中的GenerateKey方法生成私钥 privateKey, err := rsa.GenerateKey(rand.Reader, bits) if err != nil { return nil, nil, err } - // 2、通过X509标准将得到的RAS私钥序列化为:ASN.1 的DER编码字符串 privateStream := x509.MarshalPKCS1PrivateKey(privateKey) - // 3、将私钥字符串设置到pem格式块中 block1 := &pem.Block{ Type: "private key", Bytes: privateStream, } - // 通过自定义密码加密 + // use optional password if pwd != "" { block1, err = x509.EncryptPEMBlock(rand.Reader, block1.Type, block1.Bytes, []byte(pwd), x509.PEMCipherAES256) if err != nil { @@ -102,7 +99,7 @@ func RSAGenKeyWithPwd(bits int, pwd string) (pubKey, prvKey []byte, err error) { prvKey = pem.EncodeToMemory(block1) /* - 生成公钥 + generate publuicKey from privateKey */ publicKey := privateKey.PublicKey publicStream, err := x509.MarshalPKIXPublicKey(&publicKey) @@ -116,7 +113,6 @@ func RSAGenKeyWithPwd(bits int, pwd string) (pubKey, prvKey []byte, err error) { // RSADecryptWithPwd decrypt src with private key and password func RSADecryptWithPwd(src []byte, prvKey []byte, pwd string) (res []byte, err error) { - // 解码 block, _ := pem.Decode(prvKey) blockBytes := block.Bytes if pwd != "" { @@ -126,7 +122,6 @@ func RSADecryptWithPwd(src []byte, prvKey []byte, pwd string) (res []byte, err e } } privateKey, err := x509.ParsePKCS1PrivateKey(blockBytes) - // 还原数据 res, err = rsa.DecryptPKCS1v15(rand.Reader, privateKey, src) return } diff --git a/note/rsa_test.go b/note/rsa_test.go index 1266e43..2a71ce2 100644 --- a/note/rsa_test.go +++ b/note/rsa_test.go @@ -7,7 +7,7 @@ import ( /* * @CreateTime: 2021/1/13 19:46 -* @Author: hujiaming +* @Author: JemmyHu * @Description: */ diff --git a/note/varables.go b/note/varables.go index 5ad1c05..597c78b 100644 --- a/note/varables.go +++ b/note/varables.go @@ -2,7 +2,7 @@ package note /* * @CreateTime: 2021/1/12 18:47 -* @Author: hujiaming +* @Author: JemmyHu * @Description: define variables here */ @@ -74,10 +74,12 @@ func GetAppHome() string { return appHome } +// GetPubKeyName ... func GetPubKeyName() string { return pubKeyTempName } +// GetPrivateKeyName ... func GetPrivateKeyName() string { return prvTempName }