mirror of
https://github.com/wavetermdev/waveterm.git
synced 2025-01-08 19:38:51 +01:00
422338c04b
adds zsh support to waveterm. big change, lots going on here. lots of other improvements and bug fixes added while debugging and building out the feature. Commits: * refactor shexec parser.go into new package shellenv. separate out bash specific parsing from generic functions * checkpoint * work on refactoring shexec. created two new packages shellapi (for bash/zsh specific stuff), and shellutil (shared between shellapi and shexec) * more refactoring * create shellapi interface to abstract bash specific functionality * more refactoring, move bash shell state parsing to shellapi * move makeRcFile to shellapi. remove all of the 'client' options CLI options from waveshell * get shellType passed through to server/single paths for waveshell * add a local shelltype detector * mock out a zshapi * move shelltype through more of the code * get a command to run via zsh * zsh can now switch directories. poc, needs cleanup * working on ShellState encoding differences between zsh/bash. Working on parsing zsh decls. move utilfn package into waveshell (shouldn't have been in wavesrv) * switch to use []byte for vardecl serialization + diffs * progress on zsh environment. still have issues reconciling init environment with trap environment * fix typeset argument parsing * parse promptvars, more zsh specific ignores * fix bug with promptvar not getting set (wrong check in FeState func) * add sdk (issue #188) to list of rtnstate commands * more zsh compatibility -- working with a larger ohmyzsh environment. ignore more variables, handle exit trap better. unique path/fpath. add a processtype variable to base. * must return a value * zsh alias parsing/restoring. diff changes (and rtnstate changes). introduces linediff v1. * force zmodload of zsh/parameter * starting work on zsh functions * need a v1 of mapdiff as well (to handle null chars) * pack/unpack of ints was wrong (one used int and one use uint). turned out we only ever encoded '0' so it worked. that also means it is safe to change unpack to unpackUInt * reworking for binary encoding of aliases and functions (because of zsh allows any character, including nulls, in names and values) * fixes, working on functions, issue with line endings * zsh functions. lots of ugliness here around dealing with line dicipline and cooked stty. new runcommand function to grab output from a non-tty fd. note that we still to run the actual command in a stty to get the proper output. * write uuid tempdir, cleanup with tmprcfilename code * hack in some simple zsh function declaration finding code for rtnstate. create function diff for rtnstate that supports zsh * make sure key order is constant so shell hashes are consistent * fix problems with state diffs to support new zsh formats. add diff/apply code to shellapi (moved from shellenv), that is now specific to zsh or bash * add log packet and new shellstate packets * switch to shellstate map that's also keyed by shelltype * add shelltype to remoteinstance * remove shell argument from waveshell * added new shelltype statemap to remote.go (msh), deal with fallout * move shellstate out of init packet, and move to an explicit reinit call. try to initialize all of the active shell states * change dont always store init state (only store on demand). initialize shell states on demand (if not already initialized). allow reset to change shells * add shellpref field to remote table. use to drive the default shell choice for new tabs * show shelltag on cmdinput, pass through ri and remote (defaultshellstate) * bump mshell version to v0.4 * better version validation for shellstate. also relax compatibility requirements for diffing states (shelltype + major version need to match) * better error handling, check shellstate compatibility during run (on waveshell server) * add extra separator for bash shellstate processing to deal with spurious output from rc files * special migration for v30 -- flag invalid bash shell states and show special button in UI to fix * format * remove zsh-decls (unused) * remove test code * remove debug print * fix typo
235 lines
5.7 KiB
Go
235 lines
5.7 KiB
Go
// Copyright 2023, Command Line Inc.
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package sstore
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"io/fs"
|
|
"log"
|
|
"os"
|
|
"strconv"
|
|
"time"
|
|
|
|
_ "github.com/golang-migrate/migrate/v4/database/sqlite3"
|
|
_ "github.com/golang-migrate/migrate/v4/source/file"
|
|
"github.com/golang-migrate/migrate/v4/source/iofs"
|
|
_ "github.com/mattn/go-sqlite3"
|
|
sh2db "github.com/wavetermdev/waveterm/wavesrv/db"
|
|
|
|
"github.com/golang-migrate/migrate/v4"
|
|
)
|
|
|
|
const MaxMigration = 30
|
|
const MigratePrimaryScreenVersion = 9
|
|
const CmdScreenSpecialMigration = 13
|
|
const CmdLineSpecialMigration = 20
|
|
const RISpecialMigration = 30
|
|
|
|
func MakeMigrate() (*migrate.Migrate, error) {
|
|
fsVar, err := iofs.New(sh2db.MigrationFS, "migrations")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("opening iofs: %w", err)
|
|
}
|
|
// migrationPathUrl := fmt.Sprintf("file://%s", path.Join(wd, "db", "migrations"))
|
|
dbUrl := fmt.Sprintf("sqlite3://%s", GetDBName())
|
|
m, err := migrate.NewWithSourceInstance("iofs", fsVar, dbUrl)
|
|
// m, err := migrate.New(migrationPathUrl, dbUrl)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("making migration db[%s]: %w", GetDBName(), err)
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
func copyFile(srcFile string, dstFile string, notFoundOk bool) error {
|
|
if srcFile == dstFile {
|
|
return fmt.Errorf("cannot copy %s to itself", srcFile)
|
|
}
|
|
srcFd, err := os.Open(srcFile)
|
|
if notFoundOk && err != nil && errors.Is(err, fs.ErrNotExist) {
|
|
return nil
|
|
}
|
|
if err != nil {
|
|
return fmt.Errorf("cannot open %s: %v", srcFile, err)
|
|
}
|
|
defer srcFd.Close()
|
|
dstFd, err := os.OpenFile(dstFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
|
|
if err != nil {
|
|
return fmt.Errorf("cannot open destination file %s: %v", dstFile, err)
|
|
}
|
|
_, err = io.Copy(dstFd, srcFd)
|
|
if err != nil {
|
|
dstFd.Close()
|
|
return fmt.Errorf("error copying file: %v", err)
|
|
}
|
|
return dstFd.Close()
|
|
}
|
|
|
|
func MigrateUpStep(m *migrate.Migrate, newVersion uint) error {
|
|
startTime := time.Now()
|
|
err := m.Migrate(newVersion)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if newVersion == CmdScreenSpecialMigration {
|
|
mErr := RunMigration13()
|
|
if mErr != nil {
|
|
return fmt.Errorf("migrating to v%d: %w", newVersion, mErr)
|
|
}
|
|
}
|
|
if newVersion == CmdLineSpecialMigration {
|
|
mErr := RunMigration20()
|
|
if mErr != nil {
|
|
return fmt.Errorf("migrating to v%d: %w", newVersion, mErr)
|
|
}
|
|
}
|
|
if newVersion == RISpecialMigration {
|
|
mErr := RunMigration30()
|
|
if mErr != nil {
|
|
return fmt.Errorf("migrating to v%d: %w", newVersion, mErr)
|
|
}
|
|
}
|
|
log.Printf("[db] migration v%d, elapsed %v\n", newVersion, time.Since(startTime))
|
|
return nil
|
|
}
|
|
|
|
func MigrateUp(targetVersion uint) error {
|
|
m, err := MakeMigrate()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
curVersion, dirty, err := MigrateVersion(m)
|
|
if dirty {
|
|
return fmt.Errorf("cannot migrate up, database is dirty")
|
|
}
|
|
if err != nil {
|
|
return fmt.Errorf("cannot get current migration version: %v", err)
|
|
}
|
|
if curVersion >= targetVersion {
|
|
return nil
|
|
}
|
|
log.Printf("[db] migrating from %d to %d\n", curVersion, targetVersion)
|
|
log.Printf("[db] backing up database %s to %s\n", DBFileName, DBFileNameBackup)
|
|
os.Remove(GetDBBackupName()) // don't report error
|
|
os.Remove(GetDBWALBackupName()) // don't report error
|
|
err = copyFile(GetDBName(), GetDBBackupName(), false)
|
|
if err != nil {
|
|
return fmt.Errorf("error creating database backup: %v", err)
|
|
}
|
|
err = copyFile(GetDBWALName(), GetDBWALBackupName(), true)
|
|
if err != nil {
|
|
return fmt.Errorf("error creating database(wal) backup: %v", err)
|
|
}
|
|
for newVersion := curVersion + 1; newVersion <= targetVersion; newVersion++ {
|
|
err = MigrateUpStep(m, newVersion)
|
|
if err != nil {
|
|
return fmt.Errorf("during migration v%d: %w", newVersion, err)
|
|
}
|
|
}
|
|
log.Printf("[db] migration done, new version = %d\n", targetVersion)
|
|
return nil
|
|
}
|
|
|
|
// returns curVersion, dirty, error
|
|
func MigrateVersion(m *migrate.Migrate) (uint, bool, error) {
|
|
if m == nil {
|
|
var err error
|
|
m, err = MakeMigrate()
|
|
if err != nil {
|
|
return 0, false, err
|
|
}
|
|
}
|
|
curVersion, dirty, err := m.Version()
|
|
if err == migrate.ErrNilVersion {
|
|
return 0, false, nil
|
|
}
|
|
return curVersion, dirty, err
|
|
}
|
|
|
|
func MigrateDown() error {
|
|
m, err := MakeMigrate()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = m.Down()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func MigrateGoto(n uint) error {
|
|
curVersion, _, _ := MigrateVersion(nil)
|
|
if curVersion == n {
|
|
return nil
|
|
}
|
|
if curVersion < n {
|
|
return MigrateUp(n)
|
|
}
|
|
m, err := MakeMigrate()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = m.Migrate(n)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func TryMigrateUp() error {
|
|
curVersion, _, _ := MigrateVersion(nil)
|
|
log.Printf("[db] db version = %d\n", curVersion)
|
|
if curVersion >= MaxMigration {
|
|
return nil
|
|
}
|
|
err := MigrateUp(MaxMigration)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return MigratePrintVersion()
|
|
}
|
|
|
|
func MigratePrintVersion() error {
|
|
version, dirty, err := MigrateVersion(nil)
|
|
if err != nil {
|
|
return fmt.Errorf("error getting db version: %v", err)
|
|
}
|
|
if dirty {
|
|
return fmt.Errorf("error db is dirty, version=%d", version)
|
|
}
|
|
log.Printf("[db] version=%d\n", version)
|
|
return nil
|
|
}
|
|
|
|
func MigrateCommandOpts(opts []string) error {
|
|
var err error
|
|
if opts[0] == "--migrate-up" {
|
|
fmt.Printf("migrate-up %v\n", GetDBName())
|
|
time.Sleep(3 * time.Second)
|
|
err = MigrateUp(MaxMigration)
|
|
} else if opts[0] == "--migrate-down" {
|
|
fmt.Printf("migrate-down %v\n", GetDBName())
|
|
time.Sleep(3 * time.Second)
|
|
err = MigrateDown()
|
|
} else if opts[0] == "--migrate-goto" {
|
|
n, err := strconv.Atoi(opts[1])
|
|
if err == nil {
|
|
fmt.Printf("migrate-goto %v => %d\n", GetDBName(), n)
|
|
time.Sleep(3 * time.Second)
|
|
err = MigrateGoto(uint(n))
|
|
}
|
|
} else {
|
|
err = fmt.Errorf("invalid migration command")
|
|
}
|
|
if err != nil && err.Error() == migrate.ErrNoChange.Error() {
|
|
err = nil
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return MigratePrintVersion()
|
|
}
|