Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor fixes in functionality and style. #17

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion frontend/static/query.html
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,8 @@
tableContent += `<th class="mdc-data-table__cell " scope="row ">` + modules[i].Version + `</th>`
if (queryType == "ModulesByOrgName" || queryType == "ModulesByKey") {
tableContent += `<th class="mdc-data-table__cell " scope="row "><a href="` + modules[i].URL + `">URL</a>` + `</th>`
tableContent += `<th class="mdc-data-table__cell " scope="row ">` + modules[i].Summary + `</th>`
}
tableContent += `<th class="mdc-data-table__cell " scope="row ">` + modules[i].Summary + `</th>`
tableContent += `<th class="mdc-data-table__cell " scope="row ">
<div class="mdc-touch-target-wrapper ">
<button class="mdc-button mdc-button--touch " onclick="download( '` + i + `') ">
Expand Down
6 changes: 3 additions & 3 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,20 @@ func NewClient(filepath string) (*Client, error) {
token := ""
if filepath != "" {
var err error
token, err = ReadAuthToken(filepath)
token, err = readAuthToken(filepath)
if err != nil {
return nil, fmt.Errorf("failed to create a new Client, read token failed: %v", err)
}
}
return &Client{token: token}, nil
}

// ReadAuthToken takes in *filepath* of token file, reads token and returns token string.
// readAuthToken takes in *filepath* of token file, reads token and returns token string.
// This token is used when server is deployed on Google Cloud Run and only avaiable to permitted users.
// In this case, users need to include a header with identity token to get access to catalog server.
// This token can be obtained via the command `glcoud auth print-identity-token`.
// Reference: https://cloud.google.com/sdk/gcloud/reference/auth/print-identity-token.
func ReadAuthToken(filepath string) (string, error) {
func readAuthToken(filepath string) (string, error) {
token, err := ioutil.ReadFile(filepath)
if err != nil {
return "", fmt.Errorf("Read Auth token from file failed: %v", err)
Expand Down
34 changes: 17 additions & 17 deletions pkg/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
Package db contains functions related to database.
* db.go includes conneting to db, query and insertion.
* dbschema.go contains definitions of struct for db tables.
Currently it only contains Module struct.
*/
package db

Expand All @@ -40,7 +39,8 @@ const (
selectModules = `select * from modules`
// We want to ensure that user has to provide all three inputs,
// instead of deleting too many modules by mistake with some fields missing.
deleteModule = `delete from modules where orgName = $1 and name = $2 and version = $3`
deleteModule = `delete from modules where orgName = $1 and name = $2 and version = $3`

selectFeatureBundles = `select * from featureBundles`
// $4 and $5 should be assigned with the same value (the JSON data of feature-bundle).
insertFeatureBundle = `INSERT INTO featureBundles (orgName, name, version, data) VALUES($1, $2, $3, $4) on conflict (orgName, name, version) do update set data=$5`
Expand Down Expand Up @@ -161,10 +161,10 @@ func InsertModule(orgName string, name string, version string, data string) erro
return nil
}

// ReadModulesByRow scans from queried modules from rows one by one, rows are closed inside.
// readModulesByRow scans from queried modules from rows one by one, rows are closed inside.
// Return slice of db Module struct each field of which corresponds to one column in db.
// Error is returned when scan rows failed.
func ReadModulesByRow(rows *sql.Rows) ([]Module, error) {
func readModulesByRow(rows *sql.Rows) ([]Module, error) {
var modules []Module
defer rows.Close()
for rows.Next() {
Expand All @@ -177,10 +177,10 @@ func ReadModulesByRow(rows *sql.Rows) ([]Module, error) {
return modules, nil
}

// FormatQueryStr is used to generate query statement string based on query parameters.
// formatQueryStr is used to generate query statement string based on query parameters.
// * parmNames is a list of names of all non-nil query parameters.
// * baseQuery is query statement without any query parameters.
func FormatQueryStr(parmNames []string, baseQuery string) string {
func formatQueryStr(parmNames []string, baseQuery string) string {
queryStmt := baseQuery
for i := 0; i < len(parmNames); i++ {
if i == 0 {
Expand All @@ -207,7 +207,7 @@ func QueryModulesByOrgName(orgName *string) ([]Module, error) {
}

// Format query statement string based on non-nil query parameters
queryStmt := FormatQueryStr(parmNames, selectModules)
queryStmt := formatQueryStr(parmNames, selectModules)

rows, err := db.Query(queryStmt, parms...)
if err != nil {
Expand All @@ -216,7 +216,7 @@ func QueryModulesByOrgName(orgName *string) ([]Module, error) {

defer rows.Close()

return ReadModulesByRow(rows)
return readModulesByRow(rows)
}

// QueryModulesByKey queries modules by its key (name, version), it is possible that parameters are null.
Expand All @@ -238,7 +238,7 @@ func QueryModulesByKey(name *string, version *string) ([]Module, error) {
}

// Format query statement string based on non-nil query parameters
queryStmt := FormatQueryStr(parmNames, selectModules)
queryStmt := formatQueryStr(parmNames, selectModules)

rows, err := db.Query(queryStmt, parms...)
if err != nil {
Expand All @@ -247,7 +247,7 @@ func QueryModulesByKey(name *string, version *string) ([]Module, error) {

defer rows.Close()

return ReadModulesByRow(rows)
return readModulesByRow(rows)
}

// DeleteModule takes three string, orgName, name, version,
Expand All @@ -271,16 +271,16 @@ func DeleteModule(orgName string, name string, version string) error {
return nil
}

// ReadFeatureBundlesByRow scans from queried FeatureBundles from rows one by one, rows are closed inside.
// readFeatureBundlesByRow scans from queried FeatureBundles from rows one by one, rows are closed inside.
// Return slice of db FeatureBundle struct each field of which corresponds to one column in db.
// Error is returned when scan rows failed.
func ReadFeatureBundlesByRow(rows *sql.Rows) ([]FeatureBundle, error) {
func readFeatureBundlesByRow(rows *sql.Rows) ([]FeatureBundle, error) {
var featureBundles []FeatureBundle
defer rows.Close()
for rows.Next() {
var featureBundle FeatureBundle
if err := rows.Scan(&featureBundle.OrgName, &featureBundle.Name, &featureBundle.Version, &featureBundle.Data); err != nil {
return nil, fmt.Errorf("ReadFeatureBundlesByRow: scan db rows failure, %v", err)
return nil, fmt.Errorf("readFeatureBundlesByRow: scan db rows failure, %v", err)
}
featureBundles = append(featureBundles, featureBundle)
}
Expand All @@ -301,14 +301,14 @@ func QueryFeatureBundlesByOrgName(orgName *string) ([]FeatureBundle, error) {
}

// Format query statement string based on non-nil query parameters
queryStmt := FormatQueryStr(parmNames, selectFeatureBundles)
queryStmt := formatQueryStr(parmNames, selectFeatureBundles)

rows, err := db.Query(queryStmt, parms...)
if err != nil {
return nil, fmt.Errorf("QueryFeatureBundlesByOrgName failed: %v", err)
}

return ReadFeatureBundlesByRow(rows)
return readFeatureBundlesByRow(rows)
}

// InsertFeatureBundle inserts FeatureBundle into database given values of four field of FeatureBundle schema.
Expand Down Expand Up @@ -361,12 +361,12 @@ func QueryFeatureBundlesByKey(name *string, version *string) ([]FeatureBundle, e
}

// Format query statement string based on non-nil query parameters
queryStmt := FormatQueryStr(parmNames, selectFeatureBundles)
queryStmt := formatQueryStr(parmNames, selectFeatureBundles)

rows, err := db.Query(queryStmt, parms...)
if err != nil {
return nil, fmt.Errorf("QueryFeatureBundlesByKey failed: %v", err)
}

return ReadFeatureBundlesByRow(rows)
return readFeatureBundlesByRow(rows)
}
12 changes: 8 additions & 4 deletions scripts/crawl/crawl.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Program yang crawls all modules at certain commit of `https://github.com/openconfig/public`.
// Usage: yang [--path DIR] [--url URL]
// Program crawl crawls all modules at certain commit of `https://github.com/openconfig/public`.
// Usage: crawl [--path DIR] [--url URL]
//
// DIR is a comma separated list of paths that are appended as the search directory.
// If DIR appears as DIR/... then DIR and all direct and indirect subdirectories are checked.
Expand Down Expand Up @@ -146,6 +146,10 @@ func crawlModules(paths []string, url string) (map[string]*yang.Module, []string
// all modules in these directories and crawl them later.
if len(files) == 0 {
for _, path := range paths {
if _, err := os.Stat(path); err != nil {
log.Printf("WARNING: Cannot crawl in provided path: %v", err)
continue
}
names, err := traverseDir(path, url)
if err != nil {
log.Fatalf("traverse directory %s failed: %v", path, err)
Expand Down Expand Up @@ -280,15 +284,15 @@ func main() {
os.Exit(0)
}

mods, names := crawlModules(paths, url)

// Connect to DB
if err := db.ConnectDB(); err != nil {
log.Fatalf("Connect to db failed: %v", err)
stop(1)
}
defer db.Close()

mods, names := crawlModules(paths, url)

// Convert all found modules into ygot go structure of Module and insert them into database.
for _, n := range names {
module := populateModule(mods[n], n)
Expand Down
5 changes: 2 additions & 3 deletions scripts/crawl/crawloc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

go build
git clone https://github.com/openconfig/public.git
# Copy ietf directory for backup usage.
cp -r ./public/third_party/ietf .
Expand All @@ -37,7 +36,7 @@ do
cd ..
echo "./crawl -p $path -u https://raw.githubusercontent.com/openconfig/public/$commit/"

./crawl -p $path -u https://raw.githubusercontent.com/openconfig/public/$commit/
go run crawl.go -p $path -u https://raw.githubusercontent.com/openconfig/public/$commit/
cd public
# Track how many commits the script has crawled.
echo $times
Expand All @@ -50,4 +49,4 @@ done

cd ..
rm -rf public
rm -rf ietf
rm -rf ietf