github.com/fzfile/BaiduPCS-Go@v0.0.0-20200606205115-4408961cf336/pcsliner/linehistory.go (about)

     1  package pcsliner
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  )
     7  
     8  // LineHistory 命令行历史
     9  type LineHistory struct {
    10  	historyFilePath string
    11  	historyFile     *os.File
    12  }
    13  
    14  // NewLineHistory 设置历史
    15  func NewLineHistory(filePath string) (lh *LineHistory, err error) {
    16  	lh = &LineHistory{
    17  		historyFilePath: filePath,
    18  	}
    19  
    20  	lh.historyFile, err = os.OpenFile(filePath, os.O_RDWR|os.O_CREATE, 0644)
    21  	if err != nil {
    22  		return nil, err
    23  	}
    24  
    25  	return lh, nil
    26  }
    27  
    28  // DoWriteHistory 执行写入历史
    29  func (pl *PCSLiner) DoWriteHistory() (err error) {
    30  	if pl.History == nil {
    31  		return fmt.Errorf("history not set")
    32  	}
    33  
    34  	pl.History.historyFile, err = os.Create(pl.History.historyFilePath)
    35  	if err != nil {
    36  		return fmt.Errorf("写入历史错误, %s", err)
    37  	}
    38  
    39  	_, err = pl.State.WriteHistory(pl.History.historyFile)
    40  	if err != nil {
    41  		return fmt.Errorf("写入历史错误: %s", err)
    42  	}
    43  
    44  	return nil
    45  }
    46  
    47  // ReadHistory 读取历史
    48  func (pl *PCSLiner) ReadHistory() (err error) {
    49  	if pl.History == nil {
    50  		return fmt.Errorf("history not set")
    51  	}
    52  
    53  	_, err = pl.State.ReadHistory(pl.History.historyFile)
    54  	return err
    55  }