Skip to content

Commit

Permalink
Improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
zoetrope committed Apr 9, 2023
1 parent d78a52e commit b5dad60
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 22 deletions.
File renamed without changes.
6 changes: 6 additions & 0 deletions cmd/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ Constraints:
}

index, err := strconv.Atoi(args[1])
if err != nil {
return err
}
if index >= len(op.Commands) {
return fmt.Errorf("command index is too large")
}
fmt.Print(op.Commands[index])
return nil
},
Expand Down
14 changes: 12 additions & 2 deletions cmd/upload.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"encoding/json"
"fmt"
"strconv"

Expand Down Expand Up @@ -42,8 +43,17 @@ Constraints:
if err != nil {
return err
}
fmt.Println(url)
fmt.Println(offset)
j, err := json.Marshal(struct {
URL string `json:"url"`
Offset int64 `json:"offset"`
}{
URL: url,
Offset: offset,
})
if err != nil {
return err
}
fmt.Println(string(j))
return nil
},
}
Expand Down
95 changes: 75 additions & 20 deletions mop.bash
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
#!/bin/bash

MOP_CONFIG="config.json"
MOP_CONFIG="$HOME/.mop.json"
GH_MOP="gh mop --config=${MOP_CONFIG}"

# For development
MOP_CONFIG=".mop.json"
GH_MOP="go run main.go --config=${MOP_CONFIG}"
# GH_MOP="gh mop --config=${MOP_CONFIG}"

# Start an operation.
# $1: The issue number for the operation.
function mop-start() {
if [ -z $1 ]; then
echo "Error: The issue number is required."
echo "Error: The issue number is required. Usage: \"mop-start <issue number>\"."
return 1
fi

Expand All @@ -22,59 +25,84 @@ function mop-start() {
return 1
fi

if ! command -v gh &> /dev/null
then
echo "gh could not be found. Please install it with `sudo apt install gh`"
return 1
fi
if ! gh auth status &> /dev/null
then
echo "You are not logged in to github. Please run `gh auth login`"
return 1
fi
if ! command -v jq &> /dev/null
then
echo "jq could not be found. Please install it with `sudo apt install jq`"
return 1
fi
if ! command -v script &> /dev/null
then
echo "script could not be found. Please install it with `sudo apt install script`"
return 1
fi

export MOP_REPO=$(cat $MOP_CONFIG | jq -r .repository)
if [ -z "$MOP_REPO" ]; then
echo "Error: The repository name is not set in the config file."
clear-mop
mop-clear-env
return 1
fi
export MOP_DATADIR=$(cat $MOP_CONFIG | jq -r .datadir)
if [ -z "$MOP_DATADIR" ]; then
echo "Error: The data directory is not set in the config file."
clear-mop
mop-clear-env
return 1
fi
export MOP_ISSUE=$1
export MOP_STEP=0
export MOP_OFFSET=0

mkdir -p ${MOP_DATADIR}/${MOP_REPO}/${MOP_ISSUE} || return $?

$GH_MOP operation $MOP_ISSUE
status=$?
if [ $status -ne 0 ]; then
clear-mop
mop-clear-env
return $status
fi
export MOP_COMMAND_COUNT=$(cat ${MOP_DATADIR}/${MOP_REPO}/${MOP_ISSUE}/operation.json | jq '.commands | length')

script -q -f -a ${MOP_DATADIR}/${MOP_REPO}/${MOP_ISSUE}/typescript.txt
local record=${MOP_DATADIR}/${MOP_REPO}/${MOP_ISSUE}/typescript
if [ -f $record ]; then
mv $record ${record}_$(date +%Y%m%d%H%M%S)
fi
script -q -f -a $record
status=$?
if [ $status -ne 0 ]; then
clear-mop
mop-clear-env
return $status
fi
mop-clear-env
return 0
}

# Clear environment variables and functions for mop.
function clear-mop() {
# Clear environment variables
function mop-clear-env() {
unset MOP_REPO
unset MOP_DATADIR
unset MOP_ISSUE
unset MOP_STEP
unset MOP_OFFSET
unset MOP_COMMAND_COUNT

unset -f next
unset -f prev
unset -f insert
unset -f upload
unset -f list
unset -f utilities
unset -f help
}


if [ -n "$MOP_ISSUE" ]; then
export PS1="[\${MOP_REPO}#\${MOP_ISSUE}:Step\${MOP_STEP}]$ "
trap 'finish' EXIT

echo "The operation is started. Issue: $MOP_ISSUE."
echo "If you want to read the help, type \"help\"."

# Move to the next step in the operation.
function next() {
if [ $MOP_STEP -eq $(($MOP_COMMAND_COUNT - 1)) ]; then
Expand Down Expand Up @@ -103,7 +131,15 @@ if [ -n "$MOP_ISSUE" ]; then
# Upload the results of executed commands.
# The result is uploaded to the issue's comment.
function upload() {
$GH_MOP upload $MOP_ISSUE ${MOP_DATADIR}/${MOP_REPO}/${MOP_ISSUE}/typescript.txt
local result
result=$($GH_MOP upload --offset $MOP_OFFSET $MOP_ISSUE ${MOP_DATADIR}/${MOP_REPO}/${MOP_ISSUE}/typescript)
status=$?
if [ $status -ne 0 ]; then
return $status
fi
MOP_OFFSET=$(echo $result | jq -r .offset)
url=$(echo $result | jq -r .url)
echo "The result is uploaded to $url"
}

# List the commands in the current operation.
Expand Down Expand Up @@ -132,6 +168,25 @@ if [ -n "$MOP_ISSUE" ]; then
echo " MOP_REPO: The repository name."
echo " MOP_ISSUE: The issue number for the operation."
echo " MOP_STEP: The step number in the operation."
echo " MOP_OFFSET: The offset of the result in the issue comment."
echo " MOP_COMMAND_COUNT: The number of commands in the operation."
}

function finish() {
mop-clear-env

unset -f next
unset -f prev
unset -f insert
unset -f upload
unset -f list
unset -f utilities
unset -f help
unset -f finish

bind -r "\C-t"
bind -r "\C-j"
bind -r "\C-o"
}

bind -x '"\C-t":"insert"'
Expand Down
3 changes: 3 additions & 0 deletions pkg/command/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ func UploadResult(client *github.Client, issue int, filepath string, offset int6
return "", 0, fmt.Errorf("offset is too large")
}
content = content[offset:]
if len(content) == 0 {
return "", 0, fmt.Errorf("no content to upload")
}

comment := formatAsCodeBlock(content)
url, err := client.PostComment(issue, comment)
Expand Down

0 comments on commit b5dad60

Please sign in to comment.