Skip to content

Commit

Permalink
Merge pull request #25 from spiegel-im-spiegel/add-history
Browse files Browse the repository at this point in the history
Upsdate fetch and  history
  • Loading branch information
spiegel-im-spiegel authored Jan 13, 2021
2 parents e57e0c7 + 637d5f2 commit 5130c8e
Show file tree
Hide file tree
Showing 19 changed files with 381 additions and 316 deletions.
12 changes: 1 addition & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Flags:
--debug for debug
-h, --help help for ml
-i, --interactive interactive mode
--log string output log
-l, --log int history log size
-s, --style string link style [markdown|wiki|html|csv|json] (default "markdown")
-v, --version output version of ml
```
Expand All @@ -51,16 +51,6 @@ $ ml -s html https://git.io/vFR5M

Support Styles: `markdown`, `wiki`, `html`, `csv`, `json`

### logging

```
$ ml --log log.txt https://git.io/vFR5M
[GitHub - spiegel-im-spiegel/ml: Make Link with Markdown Format](https://github.com/spiegel-im-spiegel/ml)
$ cat log.txt
[GitHub - spiegel-im-spiegel/ml: Make Link with Markdown Format](https://github.com/spiegel-im-spiegel/ml)
```

### Interactive Mode

```
Expand Down
Binary file modified dependency.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 5 additions & 21 deletions ecode/errs.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,13 @@
package ecode

import "fmt"
import "errors"

//ECode is error ECodeber for "spiegel-im-spiegel/ml/ecode/..." packages
type ECode int

const (
ErrNullPointer ECode = iota + 1
ErrNoImplement
ErrInvalidRequest
var (
ErrNullPointer = errors.New("Null reference instance")
ErrNoImplement = errors.New("This style is not implementation")
ErrInvalidRequest = errors.New("invalid request")
)

var errMessage = map[ECode]string{
ErrNullPointer: "Null reference instance",
ErrNoImplement: "This style is not implementation",
ErrInvalidRequest: "invalid request",
}

func (n ECode) Error() string {
if s, ok := errMessage[n]; ok {
return s
}
return fmt.Sprintf("unknown error (%d)", int(n))
}

/* Copyright 2019-2021 Spiegel
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
44 changes: 0 additions & 44 deletions ecode/errs_test.go

This file was deleted.

78 changes: 42 additions & 36 deletions facade/facade.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package facade
import (
"bufio"
"context"
"io"
"os"
"runtime"

"github.com/spf13/cobra"
"github.com/spiegel-im-spiegel/gocli/exitcode"
"github.com/spiegel-im-spiegel/gocli/rwi"
"github.com/spiegel-im-spiegel/gocli/signal"
"github.com/spiegel-im-spiegel/ml/facade/history"
"github.com/spiegel-im-spiegel/ml/facade/interactive"
"github.com/spiegel-im-spiegel/ml/facade/options"
"github.com/spiegel-im-spiegel/ml/makelink"
Expand Down Expand Up @@ -48,54 +48,60 @@ func newRootCmd(ui *rwi.RWI, args []string) *cobra.Command {
return debugPrint(ui, err)
}

//log file
logfile, err := cmd.Flags().GetString("log")
//log size
log, err := cmd.Flags().GetInt("log")
if err != nil {
return debugPrint(ui, err)
}
var log io.Writer
if len(logfile) > 0 {
file, err := os.Create(logfile)
if err != nil {
return debugPrint(ui, err)
hist := history.NewFile(log, historyPath())
if log > 0 {
if err := mkdirHistory(); err != nil {
_ = ui.OutputErrln(err)
} else if err := hist.Load(); err != nil {
_ = ui.OutputErrln(err)
}
defer file.Close()
log = file
}

//set options
opts := options.New(signal.Context(context.Background(), os.Interrupt), style, log)
opts := options.New(style, hist)

//interactive mode
if interactiveFlag {
return interactive.Do(opts)
}

//command line
if len(args) > 0 {
for _, arg := range args {
r, err := opts.MakeLink(arg)
if err != nil {
return debugPrint(ui, err)
}
if err := ui.WriteFrom(r); err != nil {
return debugPrint(ui, err)
}
_ = ui.Outputln()
//interactive mode
if err := interactive.Do(opts); err != nil {
return debugPrint(ui, err)
}
} else {
scanner := bufio.NewScanner(ui.Reader())
for scanner.Scan() {
r, err := opts.MakeLink(scanner.Text())
if err != nil {
return debugPrint(ui, err)
//command line
ctx := signal.Context(context.Background(), os.Interrupt)
if len(args) > 0 {
for _, arg := range args {
r, err := opts.MakeLink(ctx, arg)
if err != nil {
return debugPrint(ui, err)
}
if err := ui.WriteFrom(r); err != nil {
return debugPrint(ui, err)
}
_ = ui.Outputln()
}
if err := ui.WriteFrom(r); err != nil {
return debugPrint(ui, err)
} else {
scanner := bufio.NewScanner(ui.Reader())
for scanner.Scan() {
r, err := opts.MakeLink(ctx, scanner.Text())
if err != nil {
return debugPrint(ui, err)
}
if err := ui.WriteFrom(r); err != nil {
return debugPrint(ui, err)
}
_ = ui.Outputln()
}
_ = ui.Outputln()
return scanner.Err()
}
return scanner.Err()
}

if err := hist.Save(); err != nil {
return debugPrint(ui, err)
}
return nil
},
Expand All @@ -110,7 +116,7 @@ func newRootCmd(ui *rwi.RWI, args []string) *cobra.Command {
rootCmd.Flags().BoolVarP(&interactiveFlag, "interactive", "i", false, "interactive mode")
rootCmd.Flags().BoolVarP(&debugFlag, "debug", "", false, "for debug")
rootCmd.Flags().StringP("style", "s", makelink.StyleMarkdown.String(), "link style ["+makelink.StyleList()+"]")
rootCmd.Flags().StringP("log", "", "", "output log")
rootCmd.Flags().IntP("log", "l", 0, "history log size")

return rootCmd
}
Expand Down
32 changes: 32 additions & 0 deletions facade/history.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package facade

import (
"os"

"github.com/spiegel-im-spiegel/gocli/config"
)

const logFile = "history"

func historyPath() string {
return config.Path(Name, logFile)
}

func mkdirHistory() error {
return os.MkdirAll(config.Dir(Name), 0700)
}

/* Copyright 2017-2021 Spiegel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
63 changes: 63 additions & 0 deletions facade/history/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package history

import (
"os"

"github.com/spiegel-im-spiegel/errs"
"github.com/zetamatta/go-readline-ny"
)

//HistoryFile is a history file class.
type HistoryFile struct {
*History
path string
}

var _ readline.IHistory = (*HistoryFile)(nil)

//NewFile function returns new HistoryFile instance.
func NewFile(size int, path string) *HistoryFile {
return &HistoryFile{History: New(size), path: path}
}

//Load method imports history data from file.
func (hf *HistoryFile) Load() error {
if hf == nil || hf.Size() == 0 || len(hf.path) == 0 {
return nil
}
file, err := os.Open(hf.path)
if err != nil {
return errs.Wrap(err)
}
defer file.Close()
return hf.Import(file)
}

//Save method exports history data to file.
func (hf *HistoryFile) Save() error {
if hf == nil || hf.Size() == 0 || len(hf.path) == 0 {
return nil
}
// file, err := os.Create(hf.path)
file, err := os.OpenFile(hf.path, os.O_RDWR|os.O_CREATE, 0600)
if err != nil {
return errs.Wrap(err)
}
defer file.Close()
return hf.Export(file)
}

/* Copyright 2021 Spiegel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Loading

0 comments on commit 5130c8e

Please sign in to comment.