Skip to content

Commit

Permalink
Added incremental "update" command
Browse files Browse the repository at this point in the history
  • Loading branch information
Max13 committed Jun 25, 2018
1 parent 0e0519d commit e516c7a
Showing 1 changed file with 81 additions and 8 deletions.
89 changes: 81 additions & 8 deletions dashing.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,21 @@ func commands() []cli.Command {
},
},
},
{
Name: "update",
Usage: "update a doc set",
Action: update,
Flags: []cli.Flag{
cli.StringFlag{
Name: "source, s",
Usage: "The path to the HTML files this will ingest. (Default: ./ )",
},
cli.StringFlag{
Name: "config, f",
Usage: "The path to the JSON configuration file.",
},
},
},
{
Name: "init",
ShortName: "create",
Expand Down Expand Up @@ -207,7 +222,58 @@ func build(c *cli.Context) {
if len(dashing.Icon32x32) > 0 {
addIcon(dashing.Icon32x32, name+".docset/icon.png")
}
db, err := createDB(name)
db, err := createDB(name, true)
if err != nil {
fmt.Printf("Failed to create database: %s\n", err)
return
}
defer db.Close()
texasRanger(source, source_depth, name, dashing, db)
}

func update(c *cli.Context) {
var dashing Dashing

source_depth := 0
source := c.String("source")
if len(source) == 0 {
source = "."
} else {
source_depth = len(strings.Split(source, "/"))
}

cf := strings.TrimSpace(c.String("config"))
if len(cf) == 0 {
cf = "./dashing.json"
}

conf, err := ioutil.ReadFile(cf)
if err != nil {
fmt.Printf("Failed to open configuration file '%s': %s (Run `dashing init`?)\n", cf, err)
os.Exit(1)
}

if err := json.Unmarshal(conf, &dashing); err != nil {
fmt.Printf("Failed to parse JSON: %s", err)
os.Exit(1)
}
if err := decodeSelectField(&dashing); err != nil {
fmt.Printf("Could not understand selector value: %s\n", err)
os.Exit(2)
}

name := dashing.Package

fmt.Printf("Building %s from files in '%s'.\n", name, source)

os.MkdirAll(name+".docset/Contents/Resources/Documents/", 0755)

setIgnore(dashing.Ignore)
addPlist(name, &dashing)
if len(dashing.Icon32x32) > 0 {
addIcon(dashing.Icon32x32, name+".docset/icon.png")
}
db, err := createDB(name, false)
if err != nil {
fmt.Printf("Failed to create database: %s\n", err)
return
Expand Down Expand Up @@ -328,20 +394,27 @@ func addPlist(name string, config *Dashing) {
ioutil.WriteFile(name+".docset/Contents/Info.plist", file.Bytes(), 0755)
}

func createDB(name string) (*sql.DB, error) {
func createDB(name string, fresh bool) (*sql.DB, error) {
dbname := name + ".docset/Contents/Resources/docSet.dsidx"
os.Remove(dbname)

if fresh {
os.Remove(dbname)
}

db, err := sql.Open("sqlite3", dbname)
if err != nil {
return db, err
}
if _, err := db.Exec(`CREATE TABLE searchIndex(id INTEGER PRIMARY KEY, name TEXT, type TEXT, path TEXT)`); err != nil {
return db, err
}
if _, err := db.Exec(`CREATE UNIQUE INDEX anchor ON searchIndex (name, type, path)`); err != nil {
return db, err

if fresh {
if _, err := db.Exec(`CREATE TABLE searchIndex(id INTEGER PRIMARY KEY, name TEXT, type TEXT, path TEXT)`); err != nil {
return db, err
}
if _, err := db.Exec(`CREATE UNIQUE INDEX anchor ON searchIndex (name, type, path)`); err != nil {
return db, err
}
}

return db, nil
}

Expand Down

0 comments on commit e516c7a

Please sign in to comment.