Skip to content

Commit

Permalink
feature: add mkpypkg to debug erda pygrator (erda-project#905)
Browse files Browse the repository at this point in the history
Co-authored-by: erda-bot <[email protected]>
  • Loading branch information
dspo and erda-bot authored Jul 13, 2021
1 parent edf05a2 commit e44b506
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 5 deletions.
108 changes: 108 additions & 0 deletions tools/cli/cmd/migrate_mkpypkg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package cmd

import (
"io/ioutil"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"

"github.com/erda-project/erda/pkg/database/sqlparser/pygrator"
"github.com/erda-project/erda/tools/cli/command"
)

var MigrateMkPyPkg = command.Command{
ParentName: "Migrate",
Name: "mkpypkg",
ShortHelp: "generate python package",
LongHelp: "generate python package",
Example: "erda-cli migrate mkpypkg --filename=my_script.py --mysql-host localhost --mysql-port 3306 --mysql-username root --mysql-password *** --database erda --sandbox-port 3307",
Hidden: true,
DontHideCursor: false,
Args: nil,
Flags: append(
mysqlFlags,
command.StringFlag{
Short: "",
Name: "filename",
Doc: "python script filename",
DefaultValue: "",
},
command.StringFlag{
Short: "",
Name: "requirements",
Doc: "requirements.txt file name",
DefaultValue: "",
},
command.BoolFlag{
Short: "",
Name: "commit",
Doc: "",
DefaultValue: false,
},
),
Run: RunMigrateMkPyPkg,
}

type textFile struct {
name string
data []byte
}

func (t textFile) GetName() string {
return t.name
}

func (t textFile) GetData() []byte {
return t.data
}

func RunMigrateMkPyPkg(ctx *command.Context, host string, port int, username, password, database string, sandboxPort int,
filename, requirements string, commit bool) (err error) {
logrus.Infoln("Erda Migrator generate the python package")

var developerScript = textFile{
name: filename,
data: nil,
}

developerScript.data, err = ioutil.ReadFile(filename)
if err != nil {
return errors.Wrap(err, "failed to read file")
}

var p = pygrator.Package{
DeveloperScript: developerScript,
Requirements: nil,
Settings: pygrator.Settings{
Engine: pygrator.DjangoMySQLEngine,
User: username,
Password: password,
Host: host,
Port: port,
Name: database,
TimeZone: pygrator.TimeZoneAsiaShanghai,
},
Commit: commit,
}

p.Requirements, err = ioutil.ReadFile(requirements)
if err != nil {
logrus.WithError(err).Warnln("failed to read requirements.txt, use default")
p.Requirements = []byte(pygrator.BaseRequirements)
}

return p.Make()
}
13 changes: 8 additions & 5 deletions tools/cli/cmd/migrate_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,15 @@ func RunMigrateRecord(ctx *command.Context, host string, port int, username, pas
}

insert := fmt.Sprintf(recordSQLPat, module, filename, script.Checksum(), script.Type)
fmt.Println("-- ---------------------------------------------------------------------------")
if dry {
fmt.Println("-- This is the record SQL, you can copy it and execute on your MySQL server --")
} else {
fmt.Println("-- This is the record SQL, the tool will execute it on your MySQL server -----")
}
fmt.Println(insert, ";")
fmt.Println("-- ---------------------------------------------------------------------------")
if dry {
fmt.Println("-- -------------------------------------------------------------------------")
fmt.Println("-- This is the record SQL, you can copy it and execute on you MySQL server--")
fmt.Println(insert, ";")
fmt.Println("-- -------------------------------------------------------------------------")

return nil
}

Expand Down
1 change: 1 addition & 0 deletions tools/cli/command/root_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ var (
"migrate",
"lint",
"mkpy",
"mkpypkg",
"record",
"help",
}
Expand Down

0 comments on commit e44b506

Please sign in to comment.